我的请求网址:
http://localhost:8000/api/get-filter-places/z/11,18
没问题查询
$kitchen = $request->kitchen;
$places = Place::where('name', 'like', '%' . $request->name . '%')
->whereHas('eatCategories', function($q) use($kitchen){
$q->whereIn('eat_category_id', [11,18]);
})->get();
当我写$ kitchen而不是11,18时,它只能用作11。
我的模特:
public function eatCategories(){
return $this -> belongsToMany('App\EatCategory', 'eat_category_pivots');
}
这不起作用:
$kitchen = $request->kitchen;
$places = Place::where('name', 'like', '%' . $request->name . '%')
->whereHas('eatCategories', function($q) use($kitchen){
$q->whereIn('eat_category_id', [$kitchen]);
})->get();
答案 0 :(得分:1)
您需要将代码更改为:
.
此外,您需要在网址中使用其他字符,例如,
而不是http://localhost:8000/api/get-filter-places/z/11.18
,以使其有效:
$kitchen = explode('.', $request->kitchen);
form-inline
答案 1 :(得分:1)
使用这样的分隔符|
http://localhost:8000/api/get-filter-places/z/11|18
试试这个:
$kitchen = explode('|', $request->kitchen);
$places = Place::where('name', 'like', '%' . $request->name . '%')
->whereHas('eatCategories', function($q) use($kitchen){
$q->whereIn('eat_category_id', $kitchen);
})->get();
答案 2 :(得分:1)
$kitchen = $request->kitchen;
$kitchen = explode(',', $request->kitchen);
$places = Place::where('name', 'like', '%' . $request->name . '%')
->whereHas('eatCategories', function($q) use($kitchen){
$q->whereIn('eat_category_id', $kitchen);
})->get();