我正在laravel中创建一个API的搜索,但是我的搜索给了我错误的结果。我正在尝试按地点和食物类型进行搜索。我有以下表格:使用预热过滤结果
- 食品
- 商店
- shop_food
- 用户
- 评论
这里是我的搜索代码:
public function searchShop($food, $location)
{
//
if($food == " " || $location == " "){
return $this->index();
}
//get all records where city and food are equal to
$shops = Shop::where('city', '=', $location)
->with('comments.user')
->with(['foods'=> function($query) use($food){
$query->where('name','=', 'fish pepper'); }])
->get();
//check if empty and return all
if($shops->isEmpty()){
return $this->index();
}
return $shops;
}
我的结果是在下面而不是记录位置和食物它显示所有商店过滤的位置,即使食物不匹配: