简体   繁体   English

Laravel Spatie Query Builder - 为过滤器创建 OR 条件

[英]Laravel Spatie Query Builder - create OR condition for filters

I am using laravel spatie for filtering the model i have following filters in allowed filter array我正在使用 laravel spatie 过滤模型我在允许的过滤器数组中有以下过滤器

$this->allowedFilters = [
        'name',
        AllowedFilter::exact('company_id'),
        'location',
        'summary',
        'client_name',
    ];

when I make request like当我提出要求时

past-projects?filter[company_id]=${companyId}&filter[name]=${search}&filter[summary]={company_id}

it searches for the data which exactly contains all the passed search params with AND condition and returns no results if any of passed param do not match.它使用 AND 条件搜索完全包含所有传递的搜索参数的数据,如果传递的任何参数不匹配,则不返回任何结果。 I want to get results if any of the passed param match.如果任何传递的参数匹配,我想得到结果。

Any help will be appreciated.任何帮助将不胜感激。

As per Spatie Laravel Query Builder documentation the allowedFilters() method will take the array of strings and return a result where all the parameters match.根据Spatie Laravel Query Builder 文档allowedFilters()方法将获取字符串数组并返回所有参数匹配的结果。

The bad news: Therefore, it will not work as how you are intending to use it.坏消息:因此,它不会像您打算使用的那样工作。

Spatie docs #Basic Usage 部分

What you want is Laravel's Or Where Clause .你想要的是 Laravel 的Or Where 子句

Laravel 8 文档或 Where 子句

The good news: since Spatie Laravel Query Builder extends Laravel's default Eloquent query builder you can use both Laravel queries and Spatie queries in conjunction.好消息:由于 Spatie Laravel 查询构建器扩展了 Laravel 的默认 Eloquent 查询构建器,您可以同时使用 Laravel 查询和 Spatie 查询。


Say you want to get all users from "Org ABC" and from that result you want to get all users with either the name "Jack" or the location "USA" or the client_name "John" or the summary "Hello World".假设您想从“Org ABC”中获取所有用户,并且您想从该结果中获取名称为“Jack”或位置为“USA”或客户端名称为“John”或摘要为“Hello World”的所有用户。 Then you can try那你可以试试

$result = QueryBuilder::for(Projects::class)
          ->allowedFilters(['company_id']
          ->where(function ($query) use ($name, $location, ...)
                $query
                    ->where('projects.user.name', 'like', '%$title%')
                    ->orWhere('projects.location', 'like', '%$location%')
                    ->orWhere() ....
          );

return $result

I came across this issue... Here's a way I resolved it.我遇到了这个问题......这是我解决它的一种方法。 You could also use the callback in the query builder like this...您也可以像这样在查询构建器中使用回调...

$users = QueryBuilder::for(User::class)
->allowedFilters([
'user_type',
'user_city',
AllowedFilter::exact('status')->default('new'),
AllowedFilter::callback('search', function($query, $value){
    $query->where('name', 'LIKE', '%' . $value . '%')
    ->orWhere('bio', 'LIKE', '%' . $value . '%')
    ->orWhere('state', 'LIKE', '%' . $value . '%')
    ->orWhere('address', 'LIKE', '%' . $value . '%');
})
])->get();

then the URL will look like this for example那么 URL 将如下所示

?filter[user_type]=customer&filter[search]=doe&filter[user_city]=boston

The search parameter will be used to check the name, bio, state and address for any match.搜索参数将用于检查姓名、简历、状态和地址是否匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM