可过滤 , by Abdalrhman Emad Saad , is a Laravel package that provides a structured approach to Eloquent query filtering. Instead of manual query building in controllers, it uses dedicated filter classes and multiple specialized engines to map HTTP requests to database queries.
The package's core strength lies in its modular engine architecture, allowing developers to choose the filtering style that best fits their frontend requirements.
基本用法
To get started, add the
HasFilterable
trait to your model and optionally bind a default filter class:
命名空间
应用程序\模型
;使用
App\Http\Filters\TaskFilter
;使用
Kettasoft\Filterable\Traits\HasFilterable
;使用
照亮\数据库\雄辩\模型
;班级
任务
延伸
模型{
使用
HasFilterable
;
受保护
$filterable
=
TaskFilter
::班级
;}
Now, you can apply filters directly in your controller. The package automatically detects the request parameters:
民众
功能
指数
(){
// Uses the bound TaskFilter automatically
返回
任务
::
筛选
()
->
分页
();}
Multiple Filtering Engines
Filterable includes four built-in engines that handle different request formats:
- Invokable Engine: Maps request keys to specific methods in your filter class. It supports PHP 8 annotations for casting, validation, and authorization.
- Ruleset Engine:
Handles flat field-operator-value queries for resource lists, such as finding high-stock inventory items (e.g.,
?filter[stock][gte]=500&filter[category]=electronics)。 - Expression Engine:
Extends the Ruleset engine to support deep Eloquent relationship filtering using dot notation, such as filtering support tickets by the customer's plan level (e.g.,
?filter[user.subscription.plan][eq]=premium)。 - Tree Engine:
Processes nested AND/OR JSON trees, recursively translating them into Eloquent
where和orWheregroups.
PHP 8 Annotations for Filter Logic
The Invokable engine utilizes PHP 8 attributes to handle common tasks like input sanitization and validation directly on the filter methods. This allows you to define constraints like required fields or role-based access at the method level:
班级
TaskFilter
延伸
可过滤{
受保护
$filters
=
[
'标题'
,
'优先事项'
,
'到期日'
];
受保护
功能
标题
(
Payload
$payload){
// Use asLike() for automatic wildcard wrapping
返回
$this
->
建造者
->
在哪里
(
'标题'
,
'喜欢'
, $payload
->
asLike
(
'both'
));}#[
Cast
(
‘整数’
)]#[
在
([
1
,
2
,
3
])]
受保护
功能
优先事项
(
Payload
$payload){
返回
$this
->
建造者
->
在哪里
(
'priority_level'
, $payload
->
价值);}#[
授权
(
'manage-tasks'
)]#[
必需的
]#[
日期
]
受保护
功能
due_date
(
Payload
$payload){
返回
$this
->
建造者
->
whereDate
(
'deadline'
,
'<='
, $payload
->
价值);}}
Built-in Caching System
The package features an integrated caching system designed specifically for the filter pipeline. It supports tagging, conditional caching, and scoping by user or tenant:
// Cache store inventory for 30 minutes, isolated by branch ID产品
::
筛选
()
->
缓存
(
1800
)
->
scopeByTenant
($branchId)
->
得到
();// Only cache results for guest users to reduce load on public listings列表
::
筛选
()
->
cacheWhen
(
授權
()
->
客人
(),
3600
)
->
cacheTags
([
'public_listings'
])
->
得到
();
Filterable also includes an auto-invalidation feature that can be configured to clear specific cache tags when models are created, updated, or deleted.
CLI and Debugging Tools
Filterable provides several Artisan commands to assist with development and debugging:
php artisan filterable:setup: Initial configuration and environment setup.php artisan filterable:make-filter: Generate a new filter class scaffold.php artisan filterable:test: Test a filter class with sample data strings (e.g.,--data="status=active")。php artisan filterable:inspect: View details about a filter class, including its engines and validation rules.php artisan filterable:discover: Automatically register filter classes within the application.
安装
您可以通过 Composer 安装该软件包:
作曲家
要求
kettasoft/filterable
The package requires PHP 8.1 or higher and supports Laravel 10 through 12 as of this writing. For more information, you can visit the Filterable documentation or view the source code on GitHub 。







