Laravel管道查询集合包:Laravel Pipeline Query Collection
Laravel  /  管理员 发布于 2个月前   73
Laravel Pipeline Query Collection包 包含一组用于管道的 Eloquent 查询过滤器。
https://github.com/l3aro/pipeline-query-collection
给定一个复杂的过滤器查询,代码在查询条件方面可能会有点笨拙:
$users = User::query()
->when($request->name ?? null, function($query, $name) {
$query->where('name', 'like', "%$name%");
})
->when($request->is_admin ?? null, function($query, $isAdmin) {
$query->where('is_admin', $isAdmin ? 1 : 0);
})
->when($request->created_at_from ?? null, function($query, $date) {
$query->where('created_at', '>=', $date);
})
->when($request->created_at_to ?? null, function($query, $date) {
$query->where('created_at', '<=', $date);
})
->get();
使用这个包可以写成如下:
use Baro\PipelineQueryCollection;
// users?name=Baro&is_admin=1&created_at_from=2022-06-01&created_at_to=2022-06-31
$users = Users::query()->filter([
new PipelineQueryCollection\RelativeFilter('name'),
new PipelineQueryCollection\BooleanFilter('is_admin'),
new PipelineQueryCollection\DateFromFilter('created_at'),
new PipelineQueryCollection\DateToFilter('created_at'),
])
->get();
在撰写本文时,此软件包包含以下过滤器:
Bitwise
Boolean
Date from
Date to
Exact
Relation
Relative
Scope
Sort
你可以在 GitHub 上的 l3aro/pipeline-query-collection 开始使用这个包。
https://github.com/l3aro/pipeline-query-collection
该包的作者还写了一篇更深入的文章——构建一个性感的查询过滤器
https://baro.rezonia.com/blog/building-a-sexy-query-filter
——它引导您了解包背后的想法。
转:
https://laravel-news.com/pipeline-query-collection
nkt 在
阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 用了三分钟就被禁了,直接阿里云服务器22端口都禁了..熊丽 在
安装docker + locust + boomer压测环境实现对接口的压测中评论 试试水..博主 在
阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 @test 也可能是国内大环境所至,也是好事,督促你该研究学习新技术..test 在
阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 打了一次网页,然后再也打不开了。。是阿里云的缘故吗?..博主 在
centos7中Meili Search搜索引擎安装流程步骤中评论 @鹿 执行以下命令看看你的2.27版本是否存在strin..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号