grantholle/laravel-model-filters
Composer 安装命令:
composer require grantholle/laravel-model-filters
包简介
A composable way to add filters to your model queries.
README 文档
README
A composable way to filter Laravel models. This is not exhaustive, but it can add some basic filtering to your models.
Installation
You can install the package via composer:
composer require grantholle/laravel-model-filters
By default, the package expects that filters are stored in the f key of the request. You can change this by adding the environment variable MODEL_FILTERS_KEY to your .env file.
MODEL_FILTERS_KEY=filter
Usage
The first step is registering the filters for the desired model. In the model, add the HasFilters trait and define the filters in the filters method.
use GrantHolle\ModelFilters\Enums\Component; use GrantHolle\ModelFilters\Filters\MultipleSelectFilter; use GrantHolle\ModelFilters\Filters\TextFilter; use GrantHolle\ModelFilters\Traits\HasFilters; class User extends Authenticatable implements ExistsInSis { use HasFilters; public function filters(): array { return [ TextFilter::make('search', __('Search')) // Exclude from the list of available filters (see below when not present on `availableFiltersToArray()` ->hide() // By default, the filter will try to construct the query based on the supplied operator and value. // If that doesn't meet your needs, you can define the query parameters yourself. It should // return an instance of `Illuminate\Database\Eloquent\Builder`. ->using(fn (Builder $builder, string $search) => $builder->search($search)), // The first argument is the key that will be used to filter the model. The second argument is the label TextFilter::make('first_name', __('First name')), TextFilter::make('last_name', __('Last name')), MultipleSelectFilter::make('user_type', __('Checkbox group')) // For filters that can have multiple values/choices, you can // define the options. How it's constructed is up to you, since // the frontend is implemented independently. ->options(UserType::options()), MultipleSelectFilter::make('user_type', __('Combobox')) ->withComponent(Component::combobox) ->options(UserType::options()), ]; } }
Once your filters are defined, you can get the list of the available filters by calling the availableFiltersToArray on the model. This allows you to implement the frontend however you want.
(new User())->availableFiltersToArray(); // This is the output $filters = [ [ "key" => "first_name", "label" => "First name", "component" => "text", "operators" => [ "contains" => "Contains", "not_contains" => "Doesn't contain", "starts" => "Starts with", "not_starts" => "Doesn't start with", "ends" => "Ends with", "not_ends" => "Doesn't end with", ], "props" => [], "defaultValue" => null, ], [ "key" => "last_name", "label" => "Last name", "component" => "text", "operators" => [ "contains" => "Contains", "not_contains" => "Doesn't contain", "starts" => "Starts with", "not_starts" => "Doesn't start with", "ends" => "Ends with", "not_ends" => "Doesn't end with", ], "props" => [], "defaultValue" => null, ], [ "key" => "user_type", "label" => "Checkbox group", "component" => "checkbox_group", "operators" => [ "in" => "In", "not_in" => "Not in", ], "props" => [ "options" => [ "staff" => "Staff", "guardian" => "Contact", "student" => "Student", ], ], "defaultValue" => [], ], [ "key" => "user_type", "label" => "Combobox", "component" => "combobox", "operators" => [ "in" => "In", "not_in" => "Not in", ], "props" => [ "options" => [ "staff" => "Staff", "guardian" => "Contact", "student" => "Student", ], ], "defaultValue" => [], ], [ "key" => "user_type", "label" => "Select", "component" => "combobox", "operators" => [ "in" => "In", "not_in" => "Not in", ], "props" => [ "options" => [ "staff" => "Staff", "guardian" => "Contact", "student" => "Student", ], ], "defaultValue" => [], ], ];
The request should use the key defined in the environment (by default f) along with the filter details. Take the following query string:
?f[0][key]=first_name&f[0][operator]=starts&f[0][value]=gr
This will be expanded in the request to the following:
[
[
"key" => "first_name",
"operator" => "starts",
"value" => "gr"
]
]
In your controller, you can call currentFilters on the request to obtain the filters that should be applied to the model.
public function index(Request $request) { $filters = $request->currentFilters(); $users = User::filter($filters) ->get(); // ... }
If you'd like to filter a model manually, the following structure should be used:
use GrantHolle\ModelFilters\Enums\Operator; $filters = [ [ "key" => "first_name", // The key should match the key in the filter definition "operator" => "contains", // You can also use the Operator::contains enum "value" => "an" // This is the value by which to filter ], [ "key" => "first_name", "operator" => Operator::not_starts_with, "value" => "Gr" ] ]; User::filter($filters)->pluck("first_name");
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
grantholle/laravel-model-filters 适用场景与选型建议
grantholle/laravel-model-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel-model-filters」 「Grant Holle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 grantholle/laravel-model-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grantholle/laravel-model-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 grantholle/laravel-model-filters 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Multiauth and custom grants for laravel passport
A Laravel server implementation for Altcha.
Facebook Token Request Grant for Laravel Passport
Facebook Token Request Grant for Laravel Passport
Alfabank REST API integration
Easy to use Laravel-Passport framework for social authentication
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-25