miracuthbert/laravel-eloquent-filters
Composer 安装命令:
composer require miracuthbert/laravel-eloquent-filters
包简介
An eloquent filters package for Laravel 5.8 and up
README 文档
README
A package for Laravel that can be used to filter a model's records.
How does it work?
Simply the package checks the request query for keys that match the corresponding filter keys set for the model then builds the query.
Each model has a corresponding filters file where it's filters can be registered and mapped.
Each registered filter is a unique file that contains the necessary functionality to build a query.
This means that for models that have a title column, they can share the call the same filter file hence reducing code duplication.
Installation
Use composer to install the package:
composer require miracuthbert/laravel-eloquent-filters
Setup
The package takes advantage of Laravel Auto-Discovery, so it doesn't require you to manually add the ServiceProvider.
If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php
Miracuthbert\Filters\EloquentFiltersServiceProvider::class
If you want to publish the config file use the commands below in your console
Publish Config
php artisan vendor:publish --provider=Miracuthbert\Filters\EloquentFiltersServiceProvider --tag=filters-config
Usage
Setting up a model
A filter for a model generally extends the Miracuthbert\Filters\FiltersAbstract.
It contains a list of filters that will should be applied to a the model and a map of key/value pair of filters list.
To create one:
First, create a model with php artisan make:model command.
Then create a filter file for the model using:
php artisan filter:model {name}
// example
php artisan filter:make UserFilters
// within specific namespace
php artisan filter:make Users\\UserFilters
Switch name with the model name and preferably add suffix Filters to indicate it is a model filter, eg. UserFilters
Copy and add the block of code printed out in the console, to the related model. Do not forget to pull in the required namespaces.
Using Filter in Controller
After setup above in any controller that you call the model instance, call the filter scope passing an instance of the `Illuminate\Http\Request'.
$users = User::filter($request)->get();
In case you have disabled appending of the filters query to a paginator, you could do it manually by using the filters_query helper:
$users->appends(filters_query());
// in blade view {{ $users->appends(filters_query())->links() }}
Creating a Filter
All created filters by default will be placed within, App\Filters directory (namespace).
To create a filter use:
php artisan filter:make {name}
// example
php artisan filter:make NameFilter
// within specific namespace
php artisan filter:make Users\\NameFilter
Switch name with the name of the filter, eg. NameFilter (normal filter), CreatedOrder (ordering filter)
When creating a filter, it is good to add
FilterorOrderto the name for ease of use.
You can then open the filter file and add your custom query functionality. See the filter:make command options for some preset templates.
Filters are basically blocks of code that extend
Illuminate\Database\Eloquent\Builder, so you are not limited to the preset templates.
You can pass, the following options to the filter:make command, to make use of some of the common filter templates:
column, Indicates the column the filter should be implemented onbool, Indicates if generated filter should be a boolean filter classnull, Indicates if generated filter should be a null check filter classorder, Indicates if generated filter should be an order filter classrelation, Generates a filter class for the given relationship
Registering Filters to Model Filters
After creating a filter, to use it open a model filter and register a key/value pair under the $filters field.
/** * A list of filters. * * @var array */ protected $filters = [ 'name' => NameFilter::class, 'email' => EmailFilter::class, 'created' => CreatedOrder::class, ];
Setting Default Filters
In a model filter, you can register a key/value pair under the $defaultFilters field, for filters you want to be applied by default.
keyshould be the same as the one registered on the$filtersfield,valueshould be an actual database value.
Note: Default filters should only be used for filters with fixed or unchanging values, eg. true, false
/** * A list of default filters. * * @var array */ protected $defaultFilters = [ 'created' => 'desc', ];
You can override default filters in the constructor of a model filter, by adding different checks.
// constructor if ($request->hasAny('cancelled', 'completed')) { $this->defaultFilters = [ 'upcoming' => 'false' ]; }
Console Commands
There available commands within the package:
filter:model: Create a new filter class for a modelfilter:make: Creates a new Eloquent filter class
Pass
--helpoption to the commands to get more details on using them
Security Vulnerabilities
If you discover a security vulnerability, please send an e-mail to Cuthbert Mirambo via miracuthbert@gmail.com. All security vulnerabilities will be promptly addressed.
Credits
License
The project is open-sourced software licensed under the MIT license.
miracuthbert/laravel-eloquent-filters 适用场景与选型建议
miracuthbert/laravel-eloquent-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.46k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「filters」 「eloquent-filters」 「miracuthbert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 miracuthbert/laravel-eloquent-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 miracuthbert/laravel-eloquent-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 miracuthbert/laravel-eloquent-filters 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Advanced Laravel models filtering capabilities
Simple filters for laravel
Livewire Filters is a series of Livewire components that provide you with the tools to do live filtering of your data from your own Livewire components.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
Filters extension for Nette Framework
Laravel 5 Filterable Eloquents.
统计信息
- 总下载量: 1.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 21
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-12