thejawker/laravel-interrogator

查询请求并应用 JSON API 规则到 Laravel 查询构建器。

0.1.1 2020-12-02 18:25 UTC

README

Latest Version on Packagist

安装

通过 Composer 需求此包

composer require thejawker/laravel-interrogator

使用

您可以在控制器中调用 interrogate() 任何 Laravel 模型。基本设置很简单,但不提供适当的安全性。需要时可以添加安全性。

// App/Http/Controllers/UserController.php

// GET: /users?filter[name]=john*&sort=-name
// Returns the Users where the name starts with John and is sorted DESC by name. 
public function get() {
    return interrogate(User::class)->get();
}

过滤

您可以使用过滤器向查询构建器添加 WHERE 子句。一个过滤器由 filter[column]=value 变量表示。

通配符过滤器

您可以在字符串过滤器中使用通配符运算符。这样就可以创建相当复杂的过滤器。

GET http://example.com/api/sites?filter[url]=https*/thejawker/posts/*/images

列表过滤器

可以用逗号分隔的列表作为过滤器。这将转而运行 whereInQueryBuilder 上。

GET http://example.com/api/user?files[type]=jpg,png

注意,您目前不能将此与通配符过滤器组合如下:jpg,pn*

And 或 Or

您可以指定选择器的 andor 条件。

例如,如果您想获取具有 Gmail 电子邮件地址 AND 和荷兰手机号码的 Users,您可以这样做。

GET http://example.com/api/users?filter[email]=*gmail*&filter[phone]=[and]+31*

默认情况下,使用 or 操作符,但是您可以显式地

GET http://example.com/api/users?filter[email]=*gmail*&filter[phone]=[or]+31*

数学运算

在过滤器中可以使用各种不同的数学运算符。

当然,您可以将此与 And or Or 运算符组合。

默认过滤器

您可以在 Interrogator 中方便地设置默认过滤器。这些可以被 API 消费者(客户端)覆盖。在底层,它将过滤器设置为请求。

interrogate(User::class)
    ->defaultFilters(['email' => '*@gmail.com'])
    ->get();

如果您想,您也可以预先过滤 QueryBuilder。

interrogate(User::whereType('admin'))
    ->get();

空值过滤器

有时您需要确保一列是空值。例如,您可以这样做:

GET http://example.com/api/users?filter[profile_image]=[null]

安全

对于某些模型,您可能希望防止在特定列上进行过滤。您可以通过允许选择字段来完成此操作。不允许的过滤器将引发错误。

public function get() {
    return interrogate(User::class)
        ->allowFilters(['email', 'name'])
        ->get();
}   

注意:目前不支持嵌套过滤器。

排序

排序根据 JSON 规范通过在请求中使用 sort 参数并使用列名作为值来实现。

您可以按 asc 排序

GET http://example.com/api/users?sort=name

您也可以通过添加连字符 - 来按 降序 排序

GET http://example.com/api/users?sort=-name

安全

如过滤一样,此包中内置了一些安全性。您可以通过在 Interrogator 上添加 ->allowSortBy(['email']) 来允许某些排序列。

interrogate(User::query())
    ->request($request)
    ->allowSortBy(['email'])
    ->get();   

默认排序

默认排序也可以在 Interrogator 上定义。与过滤一样;这是添加到请求上的。

public function get() {
    return interrogate(User::class)
        ->defaultSortBy('-email')
        ->get();
}   

链式快捷方式

您可以继续使用 Interrogator 进行链式操作。Paginationget 被设置为快捷方式,允许您这样做。

// Gets all the results from the database. 
interrogate(User::class)->get();

// Paginates the results using the Laravel paginator.
interrogate(User::class)->paginate(); 

// Gets the query on the Interrogator, you are 
// then free to work with it like expected.
interrogate(User::class)->query()->take(2); 

让您的生命变得更简单

interrogate() 辅助函数通过允许各种类型来尝试让您的生命变得更简单。我们尝试解决您输入以下类型时的意图。

// Illuminate\Database\Eloquent\Builder
interrogate(User::query());

// Illuminate\Database\Eloquent\Builder
interrogate(User::whereAdmin(true));

// Illuminate\Database\Eloquent\Relations\Relation;
interrogate(User::first()->posts());

// Class contstant
interrogate(User::class);

替代方案

对于所有东西都是一个非常好的 Spatie 包。我需要能够执行一些在所述包中不受支持的特定数学操作。

测试

composer test

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件