[英]Laravel - How to filter hasMany relation based on parameter?
我有一家餐廳 Model:
class Restaurant extends Model
{
public function orders()
{
return $this->hasMany('App\Order', 'id_restaurant');
}
}
與 Order 有 hasMany 關系。
我想要做的是在某些日期之間獲得帶有訂單的餐廳。
Restaurant::whereHas('orders' , function($q) use($startDate, $endDate){
$q->where('created_at', '>=', $startDate->toDateTimeString())->where('created_at', '<=', $endDate->toDateTimeString());
})
這是我嘗試過但不好的方法。 換句話說,我想通過一些參數過濾 Model 的關系。
您可以使用:
Restaurant::with(['orders'=> function($q) use($startDate, $endDate){
$q->whereBetweeen('created_at', [$startDate,$endDate]);
}])->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.