laravel 在循环中追加 wheres 以执行 AND 过滤

laravel append wheres in a loop to perform AND filtering

我正在尝试使用 $httpParamSerializer(params); 和 Laravel [=14= 为我的 Angular 应用和 Laravel 5.1 API 构建我自己的动态过滤].

我的目标是传递一组我要过滤的字段和值,并向下钻取我需要的记录。

我循环遍历 Laravel 中的字段并执行 whereIns,然后将它们分组到一个数组中,删除重复项。不幸的是,这更像是 OR 而不是 AND,因为每个 whereIn 都会进行新的搜索以匹配。

前端查询:

        var filters = {
            'review[]' : ['no', 'yes'],
            'user_id[]' : [1]
        };

HTTP URL:“http://dde-api.localhost/1.0/userquestions?review%5B%5D=no&review%5B%5D=yes&user_id%5B%5D=1

数据库:

Laravel:

    $results = [];
    // Loop through each field (`review`, `users`, etc..), then search thru array of params
    foreach ($input as $filter_field => $filters_array) {
        if ($this->validField($table, $filter_field)) {
            $res = DB::table($table)->whereIn($filter_field, $filters_array)->get();
            if (!in_array($res, $results)) {
                array_push($results, $res);
            }

我需要查询作为多个 WHERE 子句(AND,而不是 OR)循环并将 where 子句附加到每个 field ($filter_field),然后搜索匹配的字段值。

所以结果应该是用户 1 的所有 yesno 条记录。由于用户 1 没有 review: yes 的记录,它使用来自 user_id: 4 的记录。这很糟糕。

当我遍历多个字段时,如何将多个 WHERE 语句附加到一个查询?

像这样使用你的循环

$dbTbOBJ = \DB::table("table_name")
// Loop through each field (`review`, `users`, etc..), then search thru array of params
foreach ($input as $filter_field => $filters_array) {
         $dbTbOBJ->whereIn($filter_field, $filters_array);
}
$results = $dbTbOBJ->get()