goopil / eloquent-rest-filter

该软件包已被弃用且不再维护。未建议替代软件包。

一组eloquent作用域,用于实现各种REST查询字符串

0.2.2 2018-11-25 20:14 UTC

This package is auto-updated.

Last update: 2022-01-29 03:06:23 UTC


README

Latest Version on Packagist License Build Status Coverage Status Scrutinizer Code Quality StyleCI Maintainability

REST API 作用域

此软件包将一些常见的REST查询过滤器映射到 Eloquent 作用域。这主要是从 andersao/l5-repository 中的过滤器中提取出来的,但可以通过模型本身而不是通过仓库来附加。向以前的贡献者表示衷心的感谢,感谢他们出色的作品 :)

它包含不同的作用域和执行堆栈,以便简化注册。您可以单独注册任何作用域,或使用堆栈注册所有作用域。注意:如果您单独使用它们,则请求是一个必需的构造函数参数。

您可以使用 qs 来简化参数格式化

安装

安装软件包

composer require goopil/eloquent-rest-filter

对于laravel <= 5.3,添加 \Goopil\RestFilter\RestScopeProvider::class 以使用配置文件

发布配置

您可以使用 php artisan vendor:publish --provider="Goopil\RestFilter\RestScopeProvider" --tag=config 发布它

声明

要注册所有定义的查询作用域,您可以在每个查询或控制器上定义作用域,以基于路由进行工作。

在控制器中

use App\User;
use Goopil\RestFilter\RestScopes\Scopes\FullScope;

MyController extends Controller
{
    public function __construct()
    {
        // global constructor registration
        User::addGlobalScope(new FullScope);
    }

    public function index (MyCustomRequest $r)
    {
        // you can pass the current request if you use it in your context and specify
        the separator used for this controller only
        User::addGlobalScope(new FullScope($r, ';', '|'));

        // or the request will automaticaly be fetched if none are provided.
        User::addGlobalScope(new FullScope);

        return response()->json([
            data => User::all()
        ]);
    }
 }

在模型中

手动

use Illuminate\Database\Eloquent\Model as Eloquent;
use Goopil\RestFilter\RestScopes\Scopes\FullScope;

MyModel extends Eloquent
{
    public static function boot ()
    {
        parent::boot();
        static::addGlobalScope(new FullScope);
    }
}

通过特性

use Illuminate\Database\Eloquent\Model as Eloquent;
use Goopil\RestFilter\Contracts\Queryable;

MyModel extends Eloquent
{
    use Queryable;
}

查询语法

参数格式

参数支持数组参数。 http://exemple.com/api/v1/users?search[1]=John&search[2]=Tom

分隔符

函数
; 按字段名称和查询值分隔
` `

搜索

搜索功能使用 Goopil\RestFilter\Contracts\Searchable。此接口简单地指定模型上的 searchable() 方法。这将返回要搜索的字段数组。支持关系字段。默认情况下或WHERE子句已实现。如果您想强制WHERE子句,您可以在比较段上添加一个 !

searchable 方法

    static public function searchable()
    {
        return [
            'id',
            'active',
            'username',
            'email',
            'firstname',
            'lastname',
            'roles.name',
            'roles.slug',
            'roles.description',
        ];
    }
注册的可搜索字段上的单个值

http://exemple.com/api/v1/users?search[]=John%20Doe

注册的可搜索嵌套字段上的单个值

http://exemple.com/api/v1/users?search[roles.name]=administrator

在具有比较运算符的注册可搜索字段上的单个值

http://exemple.com/api/v1/users?search[]=Johns;like

在特定字段上的单个值与比较运算符

http://exemple.com/api/v1/users?search[email]=john@gmail.com;like

多字段搜索

http://exemple.com/api/v1/users?search[name]=John%20Doe&search[email]=john@gmail.com

具有每个字段比较运算符的多字段搜索

http://exemple.com/api/v1/users?search[name]=john&search;like&search[email].com;like

具有每个字段比较运算符和强制WHERE参数的多字段搜索

http://exemple.com/api/v1/users?search[name]=john&search;like&search[email].com;!like

筛选

http://exemple.com/api/v1/users?filter=id;name

名称 类型 格式
筛选 字符串 {field1} ; {value1}

排序

http://exemple.com/api/v1/users?filter=id;name&orderBy=id&sortedBy=desc

名称 类型 格式
orderBy 字符串 {field1}
sortedBy 字符串 {desc}

包含

http://exemple.com/api/v1/users?include=roles http://exemple.com/api/v1/users?include=roles;sessions http://exemple.com/api/v1/users?include=roles.permissions;sessions

名称 类型 格式
包含 字符串 {field1} ; {value1}

分页

Goopil\RestFilter\Contracts\Paginable 重写了静态 all() 方法,用于解析请求中的 pageperPage 参数,并在需要时调用 paginate() 方法。

http://exemple.com/api/v1/users?page=1 http://exemple.com/api/v1/users?page=1&perPage=20

名称 类型 默认值
整数 1
每页数量 整数 15
输出

按Laravel分页方式

{
    "total": 53,
    "per_page": "1",
    "current_page": 1,
    "last_page": 53,
    "next_page_url": "http:\/\/exemple.com\/api\/v1\/users?page=2",
    "prev_page_url": null,
    "from": 1,
    "to": 1,
    "data": [{
      ...
     }]
}

in notIn

简单

http://exemple.com/api/v1/users?in=1;2;3;4;5;6

名称 类型 格式
in ids 分隔符字符串或整数数组
exclusive in notIn 筛选器

notIn 数组比 in 数组具有优先级 http://exemple.com/api/v1/users?in=1;2;3;4;5;6&notIn=2

 [
     { "name": "user1", "id": 1 },
     { "name": "user3", "id": 3 },
    ...
 ]
名称 字段 格式
in id 分隔符字符串或整数数组
notIn id 分隔符字符串或整数数组

范围选择器

http://exemple.com/api/v1/users?offset=10 http://exemple.com/api/v1/users?limit=20 http://exemple.com/api/v1/users?offset=10&limit=20

名称 字段 格式 默认值
offset id 整数 1
limit id 整数 15

错误

如果您发现错误或想报告某些内容,请提交一个问题。

贡献

欢迎贡献。只需分叉并提交PR。