使用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
相关产品推荐
相关产品推荐

