我想从laravel中的急切加载进行查询。我想从一个关系中查询一些东西所以我有这些查询:
$parcels = CustomerParcelDelivery::with('parcel')->where('courier_id','=',$id)
->orWhere('delivery_date','=',$today)
->select('parcel.owner_id')
->get() ;
但是我收到此错误:未找到列:1054未知列' parcel.owner_id')
答案 0 :(得分:0)
急切加载包裹的所有者
$parcels = CustomerParcelDelivery::with(['parcel', 'parcel.owner'])
->where('parcel.courier_id','=',$id)
->orWhere('parcel.delivery_date','=',$today)
->select('parcel.owner_id')
->get();
修改强>
在这种情况下,您可以使用whereHas
:
$parcels = CustomerParcelDelivery::whereHas('parcel.owner', function ($query) use ($id) {
$query->where('owner_id', $id);
})->orWhere('parcel.delivery_date','=',$today)
->get();
答案 1 :(得分:0)
此处(' parcel')是一个实名。 确保包裹关系存在于" CustomerParcelDelivery"模型
Reffer:https://laravel.com/docs/5.5/eloquent-relationships#eager-loading
尝试使用以下代码:
$parcels = CustomerParcelDelivery::with('parcel:id,owner_id')
->where('courier_id','=',$id)
->orWhere('delivery_date','=',$today)
->get();