arielsand/eloquent-api-filter

从API调用URL直接过滤Monolog/Eloquent查询的出色且简单的方式

v1.1.5 2020-09-30 14:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:31:18 UTC


README

从API调用URL直接过滤Monolog/Eloquent查询的出色且简单的方式

安装

composer require arielsand/mongodb-eloquent-api-filter-mapping

使用示例

使用特性

class UserController extends Controller
{  
    
    use Arielsand\MoloquentApiFilter\Traits\FiltersMoloquentApi;
    
    public function index(Request $request)
    {
        $users = User::query();
        
        return $this->filterApiRequest($request, $users);
    }
}

使用类

use Arielsand\MoloquentApiFilter\MoloquentApiFilter;

class UserController extends Controller
{    
    public function index(Request $request)
    {
        $query = User::query();
        
        $filtered = (new MoloquentApiFilter($request, $query))->filter(['value' => 'address'], true);
        
        return $filtered->get();
    }
}

文档

新在v1.1

通过and/or链式调用多个查询
.../model?filter[field]=like:*val1:and:like:val2*:or:null

将导致

SELECT * FROM models 
WHERE (
            field LIKE '%val1' 
        AND field LIKE 'val2%'
      )
      OR field IS NULL 
添加特性

现在您可以使用Matthenning\EloquentApiFilter\Traits\FiltersEloquentApi通过以下方式简单地过滤请求:

$this->filterApiRequest($request, $query);

URL语法

.../model?filter[field]=operator:comparison

.../model?filter[field]=operator

操作符

like, notlike, today (对于时间戳), nottoday (对于时间戳), null, notnull, ge (大于等于), gt (大于), le (小于等于), lt (小于), eq (等于)

示例查询

匹配所有以Rob开头且已故为空的实体

.../users?filter[name]=like:Rob*&filter[deceased]=null

对同一字段的多个过滤器可以链式调用。匹配所有created_at在2016-12-10和2016-12-08之间的实体

.../users?filter[created_at]=lt:2016-12-10:and:gt:2016-12-08

通过点符号过滤相关模型的字段。匹配所有Post名称包含"API"的Users的Post

.../users?filter[posts.name]=like:*API*

过滤时间戳。匹配所有今天生日的用户

.../users?filter[birthday]=today

限制和排序。匹配年龄为21岁或以上的前10个用户,按名称升序排序

.../users?filter[age]=ge:21&order[name]=asc&limit=10

已知问题

  • 按相关字段排序尚不可用。