jwohlfert23 / laravel-api-query
提供 Laravel 范围,用于从请求查询参数构建查询
4.0.0
2024-03-19 01:15 UTC
Requires
- php: ^8.2
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- kirschbaum-development/eloquent-power-joins: ^3.4
- nesbot/carbon: ~2
- symfony/http-foundation: ^7.0
Requires (Dev)
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0
README
安装
composer require jwohlfert23/laravel-api-query
用法
此包实现为一个特质,提供了 buildFromRequest
范围。
use Jwohlfert23\LaravelApiQuery\BuildQueryFromRequest; class Post { use BuildQueryFromRequest; }
Post::buildFromRequest()->get();
?sort=-id,name
等同于
Post::orderByDesc('id')->orderBy('name');
?filter[name]=Bobby&filter[author.name][contains]=Bob
等同于
Post::where('name', 'Bobby')->whereHas('author', function($q) { $q->where('name', 'like', '%Bob%'); });
注意:此包不使用 "whereHas",而是内部执行左连接。然而,结果应与上述代码相同。
过滤器默认使用 "等于" 运算符。以下是可用于过滤的运算符(上述使用 "包含")。
- eq (=)
- gt (>)
- gte (>=)
- lt (<)
- lte (<=)
- contains
?with=author,comments
等同于
Post::with('author','comments');