我有4个型号。
User.php
public function shipments() {
return $this->hasMany('App\Shipment', 'from_user_id');
}
Shipment.php
public function statuses() {
return $this->hasMany('App\StatusHistory', 'model_id');
}
User
有很多Shipment
。
Shipment
通过Status
有很多StatusHistory
个实例。
如何在Shipment
模型中具有特定ID值的User
的所有StatusHistory
值获得Eloquent关系?
答案 0 :(得分:2)
你可以做到
$user = User:find($id);
$shipments = $user->shipments()->whereHas('statuses', function ($query) {
//Select all the shipments for this user where `StatusHistory` id is 1
$query->where('id', 1);
})->get();