You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Laravel删除数据库记录时遇报错:Property [customer_id]不存在于集合实例

解决Laravel删除记录时“Property [customer_id] does not exist on this collection instance”错误

错误根源是你获取的$customer是集合(Collection),而非单个模型实例。集合是多条记录的集合体,没有customer_id属性,只有单个模型对象才具备该属性。

修正方法:

  • 将查询语句中的get()替换为first(),确保返回单个实例:
    $customer = Customer::where('your_condition', $value)->first();
    
  • 若根据主键查询,直接使用find()更简便:
    $customer = Customer::find($your_id);
    
  • 执行删除前建议先判断实例是否存在,避免空指针错误:
    if ($customer) {
        $customer->delete();
    }
    

检查你的查询代码,确认返回的是单个模型对象而非集合,就能解决这个错误。

内容的提问来源于stack exchange,提问作者Chandani Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:17:10