aqjw/filterable
Composer 安装命令:
composer require aqjw/filterable
包简介
The `Filterable` package is designed for Laravel 8.x+ and provides tools for creating and managing filters for Eloquent models. It makes it easy to scale filters and apply them to filter records.
README 文档
README
The Filterable package is designed for Laravel 8.x+ and provides tools for creating and managing filters for Eloquent models.
It makes it easy to scale filters and apply them to filter records.
Get Started 🚀
Installation
The package can be easily installed via Composer using the following command:
composer require aqjw/filterable
Adding the HasFilters Trait
After installation, add the HasFilters trait to the model that you want to filter:
use Aqjw\Filterable\HasFilters; class Product { use HasFilters;
Usage
Create a filter
To create a new filter, use the Artisan make:filter command:
php artisan make:filter ByPrice
You can also specify the column to be filtered:
php artisan make:filter ByPrice --column=retail_price
In addition, you can specify a group for the filter:
php artisan make:filter ByCategory --group=Product
The filter will then be created in the following group folder:
\App\Filters\Product\ByCategory::class
Applying Filters
Once you have created your filters, you can apply them to your model:
Product::filters([ \App\Filters\ByPrice::class, \App\Filters\BySalePrice::class, ])->get();
You can use the or operator to apply multiple filters:
Product::filters([ \App\Filters\ByPrice::class, 'or', \App\Filters\BySalePrice::class, ])->get();
If you need to group filters, you can wrap them in an array:
Product::filters([ [ \App\Filters\ByCategory::class, \App\Filters\Product\ByPrice::class, ], 'or', [ \App\Filters\BySubCategory::class, \App\Filters\BySalePrice::class, ], ])->get();
Checking Request Parameters with the key Method
The key method in the filter class is used to check if a certain key exists in the request parameters. If the key is not present, the filter will not be applied.
Here is an example of how the key method can be used:
use Aqjw\Filterable\Filter; class ByPrice extends Filter { public function key() { // only apply the filter if 'price' is present in the request return 'price'; } public function apply($query, $value) { $query->where('price', $value); } }
In this example, the ByPrice filter will only be applied if the price key is present in the request parameters.
Overriding the isActive Method
If you want to force a filter to be applied even if the corresponding key is not present in the request parameters, you can override the isActive method in the filter class.
Here is an example of how the isActive method can be used:
use Aqjw\Filterable\Filter; class ByCategory extends Filter { public function key() { // only apply the filter if 'category' is present in the request return 'category'; } public function isActive($request) { // always force the filter to be applied, // even if 'category' is not present in the request. // this will be useful when we want to apply the filter by default, // for example, when displaying all products in a certain category return true; } public function apply($query, $value) { if ($value) { // apply the filter based on the value of 'category' in the request $query->where('category', $value); } else { // if 'category' is not present in the request, default to showing products in the root category (category ID of 1) $query->where('category', 1); } } }
In this example, the ByCategory filter will always be applied, even if the category key is not present in the request parameters. If the category key is present, the filter will be applied based on its value. If the category key is not present, the filter will default to showing products in the root category (category ID of 1).
License
The Filterable package is open-sourced software licensed under the MIT License. Please see the License File for more information.
Contributing
Contributions are welcome! If you would like to contribute to this project, please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/your-feature) - Make your changes
- Push your branch to your forked repository (
git push origin feature/your-feature) - Open a pull request
aqjw/filterable 适用场景与选型建议
aqjw/filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 106 次下载、GitHub Stars 达 15, 最近一次更新时间为 2023 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「laravel」 「filter records」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aqjw/filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aqjw/filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aqjw/filterable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
A simple Cake3 plugin to slug fields and find records by slug.
Filament package to prevent the update of outdated records.
Symfony DataGridBundle
Triggers events for database records.
Data provider for yii2
统计信息
- 总下载量: 106
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-17