Laravel在关系表上雄辩的查询 [英] Laravel eloquent query on a relational table

查看:84
本文介绍了Laravel在关系表上雄辩的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel口才很陌生,我正在尝试编写一个嵌套查询来获取食物和食物的数据

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

我有两个表,一个是'food_list',主要包含'title','type' 类型为int 0/1,0表示蔬菜,1表示非蔬菜 我有另一张桌子resturent _food 其中有restaurant_id, food_id,price, image

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

现在我想获取仅是蔬菜的tge resturent的食物清单

Now i want to get food list of tge resturent which is only veg

所以我写了这样的查询

RestaurantFood:with(['foodDetail'=>function($q){
$q->where('type',0);}])->where('id',$id)->get();

但这给出了错误的答案

任何人都可以帮我吗

推荐答案

使用whereHas():

RestaurantFood::with('foodDetail')->whereHas('foodDetail',function($q){
         $q->where('type',0);
})->where('id',$id)->get();

这篇关于Laravel在关系表上雄辩的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆