最近我正在尝试让我的 api 过滤工作。我需要像这样过滤我的产品:http://localhost/search?feature_id=1,2,3,4,5...
如果我只发送 1 个 ID,一切都很好。但如何让它以这种方式工作呢?
这是我的 Controller :
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
]);
}
最佳答案
使用explode()制作 id
数组。
$ids = explode(",",$request->get('feature_id'));
$query = $query->whereIn('feature_id', $ids);
关于php - Laravel/Lumen api 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49625444/