Laravel 对关系表的雄辩查询 [英] Laravel eloquent query on a relational table
本文介绍了Laravel 对关系表的雄辩查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 laravel eloquent 还很陌生,我正在尝试编写一个嵌套查询来获取食物和餐厅食物的数据
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 餐厅的食物清单
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();
但这给出了错误的答案
谁能帮帮我
推荐答案
使用 whereHas()
:
RestaurantFood::with('foodDetail')->whereHas('foodDetail',function($q){
$q->where('type',0);
})->where('id',$id)->get();
这篇关于Laravel 对关系表的雄辩查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文