esign/laravel-query-filters
Composer 安装命令:
composer require esign/laravel-query-filters
包简介
Apply filters to Laravel's query builder.
关键字:
README 文档
README
This package allows you to easily apply filters to Laravel's query builder by abstracting filter logic into dedicated classes.
Installation
You can install the package via composer:
composer require esign/laravel-query-filters
The package will automatically register a service provider.
Usage
Preparing your model
To apply filters to your model you may use the Esign\QueryFilters\Concerns\Filterable trait:
use Esign\QueryFilters\Concerns\Filterable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use Filterable; }
Applying filters
After applying the trait to your model, a query scope filter will be available, that accepts an array of possible filters:
use App\Models\Filters\TitleFilter; use App\Models\Filters\BodyFilter; Post::filter([ TitleFilter::class, BodyFilter::class, ])->get();
The filter scope will send the query builder through an array of filters.
To pass the query builder to the next filter, you should call the $next callback with the $query.
You're not limited to only using string based classes as filters, you can pass actual instances, callbacks, or pass parameters along with your class based string:
use App\Models\Filters\TitleFilter; use Closure; use Illuminate\Database\Eloquent\Builder; Post::filter([ // Class strings TitleFilter::class, // Class strings that pass a parameter to the handle method TitleFilter::class . ':dogs', // Class instance with a constructor parameter new TitleFilter('dogs'), // Use a callback function (Builder $query, Closure $next): Builder { $query->where('title', 'like', '%dogs%'); return $next($query); }, ])->get();
Defining default filters
In case you do not provide an array of filter items, you may define a set of default filters on your model:
use App\Models\Filters\TitleFilter; use Esign\QueryFilters\Concerns\Filterable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use Filterable; public function getFilters(): array { return [ TitleFilter::class, ]; } }
You may now call the filter scope without passing an array:
Post::filter()->get();
Creating filters
To create a filter class you may use the make:filter Artisan command:
php artisan make:filter TitleFilter
namespace App\Models\Filters; use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; class TitleFilter { public function __construct(protected Request $request) {} public function handle(Builder $query, Closure $next): Builder { $query->where('title', 'like', '%' . $this->request->query('search') . '%'); return $next($query); } }
Method filters
This package also ships with a handy MethodFilter class that allows you to define filters for query string parameters as methods.
Imagine we have a request that filters a list of posts with the following query string: ?published_at=2022-01-01&title=dogs.
We could create a PostFilter that extends the MethodFilter class with the camelcased method names:
use Esign\QueryFilters\Filters\MethodFilter; use Illuminate\Database\Eloquent\Builder; class PostFilter extends MethodFilter { public function title(mixed $value): Builder { return $this->query->where('title', 'like', "%$value%"); } public function publishedAt(mixed $value): Builder { return $this->query->where('published_at', '=', $value); } }
By default, query string parameters that contain an empty value won't be called.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
esign/laravel-query-filters 适用场景与选型建议
esign/laravel-query-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.01k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「esign」 「query-filter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 esign/laravel-query-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 esign/laravel-query-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 esign/laravel-query-filters 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.
A laravel package to make your eloquent models translatable.
Manage SEO tags within your Laravel application
Load translations from the database or other sources.
Associate files with your Laravel Models
Generate slugs when saving UnderscoreTranslatable models
统计信息
- 总下载量: 14.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-15