问题
i am fairly new to laravel eloquent and i am trying to write a nested query to get data of food and food of resturent
I have two table one is 'food_list'
which mainly contain 'title','type'
Type is int 0/1, 0 means veg and 1 means non veg
Another table i have resturent _food
Which has restaurant_id, food_id,price, image
Now i want to get food list of tge resturent which is only veg
So i wrote a query like this
RestaurantFood:with(['foodDetail'=>function($q){
$q->where('type',0);}])->where('id',$id)->get();
But this giving wrong answer
Can anyone please help me ty
回答1:
Use whereHas()
:
RestaurantFood::with('foodDetail')->whereHas('foodDetail',function($q){
$q->where('type',0);
})->where('id',$id)->get();
来源:https://stackoverflow.com/questions/48860282/laravel-eloquent-query-on-a-relational-table