Laravel eloquent 关系查询 table
Laravel eloquent query on a relational table
我是 laravel eloquent 的新手,我正在尝试编写一个嵌套查询来获取食物和餐厅食物的数据
我有两个table一个是'food_list'
主要包含'title','type'
类型是 int 0/1,0 表示蔬菜,1 表示非蔬菜
另一个 table 我有 resturent _food
其中有 restaurant_id, food_id,price, image
现在我想获取只有蔬菜的 tge 餐厅的食物清单
所以我写了这样的查询
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 eloquent 的新手,我正在尝试编写一个嵌套查询来获取食物和餐厅食物的数据
我有两个table一个是'food_list'
主要包含'title','type'
类型是 int 0/1,0 表示蔬菜,1 表示非蔬菜
另一个 table 我有 resturent _food
其中有 restaurant_id, food_id,price, image
现在我想获取只有蔬菜的 tge 餐厅的食物清单
所以我写了这样的查询
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();