Laravel eloquent query on a relational table

对着背影说爱祢 提交于 2019-12-24 22:31:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!