Laravel7关联模型Accessor报错:Trying to get property 'pivot' of non-object
Laravel7多对多关联中「Trying to get property 'pivot' of non-object」错误排查
项目采用Laravel7框架,订单表(orders)与商品表(products)为多对多关联关系。以下是Order模型的代码:
public function products() { return $this->belongsToMany('App\Product') ->withPivot('quantity', 'unit_discount', 'unit_price'); } public function getTotalGrossPriceAttribute() { $totalGrossPrice = 0; foreach ($this->products as $product) { $totalGrossPrice += ($product->pivot->quantity * ($product->pivot->unit_discount + $product->pivot->unit_price)); } return $totalGrossPrice; }运行时出现错误:
Trying to get property 'pivot' of non-object,求告知代码中具体问题。
问题原因及解决方案
1. 关联集合中存在空模型
当$this->products集合里出现null元素时,访问$product->pivot就会触发该错误。常见场景:
- 多对多中间表(默认
order_product)存在无效外键,比如product_id对应的商品已被删除,但中间表记录未清理 - 延迟加载关联时,某次请求中关联数据未正确加载
解决办法:
- 清理中间表中无效的
product_id记录,确保所有外键都对应存在的商品数据 - 修改访问器,遍历前过滤空模型:
public function getTotalGrossPriceAttribute() { $totalGrossPrice = 0; // 使用filter()过滤集合中的空元素 foreach ($this->products->filter() as $product) { $totalGrossPrice += ($product->pivot->quantity * ($product->pivot->unit_discount + $product->pivot->unit_price)); } return $totalGrossPrice; }
2. 关联未正确加载pivot数据
虽然你在关联中声明了withPivot,但如果查询订单时未预加载关联,或手动查询时遗漏pivot字段,会导致pivot属性未被初始化。
解决办法:
- 查询订单时强制预加载商品关联:
$order = Order::with('products')->find($orderId);
- 或在访问器中确保关联已加载:
public function getTotalGrossPriceAttribute() { $totalGrossPrice = 0; // 检查关联是否已加载,未加载则主动加载 if (!$this->relationLoaded('products')) { $this->load('products'); } foreach ($this->products->filter() as $product) { $totalGrossPrice += ($product->pivot->quantity * ($product->pivot->unit_discount + $product->pivot->unit_price)); } return $totalGrossPrice; }
3. 中间表字段或表名不匹配
检查中间表是否存在quantity、unit_discount、unit_price字段,字段名是否和withPivot中声明的完全一致(注意大小写,Laravel默认使用蛇形命名);如果中间表不是默认的order_product,需要在关联中指定表名。
解决办法:
- 核对数据库中间表的字段名,确保与
withPivot参数一致 - 自定义中间表名时,在关联方法中指定:
return $this->belongsToMany('App\Product', '自定义中间表名') ->withPivot('quantity', 'unit_discount', 'unit_price');
内容的提问来源于stack exchange,提问作者Solmaz Ahmadi
相关产品推荐
相关产品推荐

