【发布时间】:2016-11-04 15:02:48
【问题描述】:
我有一个通过order_items 数据透视表与书籍(书籍模型)相关的订单(订单模型)表:
public function books()
{
return $this->morphedByMany('App\Models\Book', 'order_item')
->withPivot(['quantity', 'presenter_id', 'price', 'portion', 'settlement_id']);
}
我需要过滤他们的presenter_id 在数据透视表(order_items) 中为空的结果。
但是没有像 wherePivotNull 这样的方法。我也尝试了以下解决方案(reference)但没有机会:
public function books()
{
return $this->morphedByMany('App\Models\Book', 'order_item')
->withPivot(['quantity', 'presenter_id', 'price', 'portion', 'settlement_id'])
->getQuery()->whereNull('order_items.presenter_id')->get();
}
它抛出这个异常:
Relationship 方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象(查看:E:\xampp\htdocs\pnu\resources\views\orders\table.blade.php)(查看:E:\ xampp\htdocs\pnu\resources\views\orders\table.blade.php)
【问题讨论】:
-
您的 whereNull 中有错字:“presneter_id”。
-
谢谢,已修改。当然这个问题与那个无关
-
值得一试。 :) 希望你能找到错误。
-
你试过没有
->get()结尾还是使用wherePivot()方法? -
Yes.wherePivot 不接受匿名函数,就像我们可以用查询生成器做的那样,它只接受
column,operator,valueTriple
标签: laravel pivot-table relationship