【发布时间】:2017-04-09 18:08:58
【问题描述】:
我正在 laravel 中创建一个 API 搜索,但我的搜索给了我错误的结果。我正在尝试按位置和食物类型进行搜索。我有以下表格:
- 食物
- 商店
- shop_food
- 用户
- cmets
这是我的搜索代码:
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;
}
【问题讨论】: