php - Laravel/Lumen api 过滤器

标签 php laravel api lumen

最近我正在尝试让我的 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/

相关文章:

javascript - 使用 javascript 或 jquery 自动将类添加到特定的 Accordion 菜单

php - 如何从codeigniter项目中的url中删除index.php

Laravel + 碳 + 时区

java - 反射api中object.getClass()方法的使用

api - 跟踪 Google map API 上的标记点击 - 分析

php - 从类问题导出对象

netbeans - Netbeans 的 Laravel 自动格式化

php - Cloudflare 上的 Laravel 在表单错误上产生 502 Bad Gateway

c# - 使用 C# 的 Nuget 包下载计数

php - 赞成/反对投票脚本