最近我正在努力让我的api过滤工作。我需要像这样过滤我的产品:http://localhost/search?feature_id=1,2,3,4,5...
如果我只发送1个ID,一切都很好。但是如何以这种方式使它工作?
这是我的控制者:
public function search2(\Illuminate\Http\Request $request) {
$query = DB::table('tlt_product_features');
if ($request->has('feature_id') ) {
$query = $query->whereIn('feature_id', [$request->get('feature_id')]);
}
$products = $query->get();
return response()->json([
'products' =>$products
]);
}
答案 0 :(得分:4)
使用explode()制作Dependency Injection
数组。
class func getPlayerStatuses(sportName: String) -> Promise<[NSDictionary]> {
let path = "api/sports/player-status/\(sportName)/"
return API.get(path).then { (json: NSDictionary) -> [NSDictionary] in
let playerUpdates: [NSDictionary] = try json.get("player_updates")
return playerUpdates
}
}
答案 1 :(得分:0)
要在Laravel / Lumen一侧获得一个开箱即用的阵列,你必须以这种方式发送阵列:
http://localhost/search?feature_id[]=1&feature_id[]=2&feature_id[]=3...
在像PHP这样的弱类型语言中,[]实际上被用作内部解决方案,以便能够获得多值参数。您还可以指定索引:
http://localhost/search?feature_id[0]=1&feature_id[1]=2&feature_id[2]=3...
然后你可以在你的控制器中使用:
if ($request->filled('feature_id')) {
// You could also check that you have a php array : && is_array($request->input('feature_id'))
// And that it's not an empty array : && count($request->input('feature_id'))
$query = $query->whereIn('feature_id', $request->input('feature_id'));
}