mehdirajabi/filterable
Composer 安装命令:
composer require mehdirajabi/filterable
包简介
Filter eloquent model by array query strings
README 文档
README
Introduction
The Laravel Filterable Trait is a powerful package providing a reusable trait for easily implementing filter functionality in your Laravel Eloquent models. It enables filtering query results based on multiple parameters, making it simpler to create dynamic and flexible APIs or data filtering mechanisms in your applications.
Features
- Easily implement filtering on Eloquent models.
- Filter data based on various query parameters.
- Support for multiple filter types, such as exact matches, partial matches, ranges, etc.
- Customizable and extensible for adding new filter types.
- Improves code readability and maintainability by separating filtering logic from the main model.
Installation
You can install the package via Composer by running the following command:
composer require mehdirajabi/filterable
Usage
1. Apply the Filterable Trait to Your Eloquent Model
Open the Eloquent model where you want to apply the filtering functionality and add the Filterable trait:
use Mehdirajabi\Filterable\Filterable; class User extends Model { use Filterable; }
In your controller:
class UserController extends Controller { public function index() { return User::filter()->actived()->get(); } }
2. How Does Filterable Work?
Two query strings, filterColumns and filterValues, are used, both being arrays. The default operator for all columns is the "like" operator.
For example:
http://test.com/api/user/users?filterColumns[0]=name&filterValues[0]=Alex
3. Relational Filter
To filter on a relation, add the column name as nested. For example, if a user has a relation with a profile and the profile has a 'sex' column, use the following convention:
use Mehdirajabi\Filterable\Filterable; class User extends Model { use Filterable; public function profile() { return $this->hasOne(UserProfile::class); } }
http://test.com/api/user/users?filterColumns[0]=profile.sex&filterValues[0]=male
4. Change Filter Mode Operator
To change the operator of the 'status' column, define the filterMode property:
use Mehdirajabi\Filterable\Operators\FilterEqual; class User extends Model { protected $filterMode = [ 'status' => FilterEqual::class ]; }
5. Change Operator in Controller
use Mehdirajabi\Filterable\Operators\FilterBetween; class UserController extends Controller { public function index() { return User::filter(['id' => FilterBetween::class])->paginate(100); } }
Between Operator
use Mehdirajabi\Filterable\Operators\FilterBetween; class User extends Model { protected $filterMode = [ 'created_at' => FilterBetween::class ]; }
http://test.com/users?filterColumns[0]=created_at&filterValues[0]=2023-06-23 00:00:00, 2023-06-24 23:59:59
mehdirajabi/filterable 适用场景与选型建议
mehdirajabi/filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mehdirajabi/filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mehdirajabi/filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-25