mjedari/larafilter
Composer 安装命令:
composer require mjedari/larafilter
包简介
Simple laravel query string based filter package
README 文档
README
Laravel Query String Filter
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
Quick start
Create a filter
php artisan make:filter country
Implement your filter logic
In the filter class which is created, implement your login in the apply() method.
In order to get query value just use $this->value. We retrieved it for you from your request.
... public function apply(Builder $builder) { return $builder->where('country', $this->value); } ...
Register filter class for model
Before registering you should use Filterable trait in your model.
use Filterable; ... ... protected static $filters = [ Country::class, ];
Use it!
// All registered filtered are available through this method: User::filter()->get(); // Only Specific registered filter is available and executable: User::filterThrough([Country::class])->get();
Requirements
- PHP 7.4 – 8.4
- Laravel 8, 9, 10, 11, 12 and 13
Installation
You can install the package via composer:
composer require mjedari/larafilter
Then you can publish config file:
php artisan vendor:publish --provider "Mjedari\Larafilter\LarafilterServiceProvider"
Usage
Initiation
Its simple.First create a filter by this command:
php artisan make:filter filter-name"
Command will create a class under the default directory App\Filters :
namespace App\Filters; use Illuminate\Database\Eloquent\Builder; use Mjedari\Larafilter\Filters\FilterContract; class Active extends FilterContract { public function apply(Builder $builder) { // TODO: Implement apply() method. } public function options() { // TODO: Implement options() method. } /* * Set rules on the query string */ public function rules() { return [ // ]; } }
Your filter logic would be implemented in the apply() method:
public function apply(Builder $builder) { return $builder->where('avtive', $this->value); }
The important thing is that you have access query string value by $this->value in your filter class.
Using
For Which model you want to filter you should add Filterable trait in it.
class User extends Authenticatable { use Filterable; . . .
Then add related filters that you created. It should be static property:
use App\Filters\Active; use App\Filters\City; class User extends Authenticatable { use Filterable; protected static $filters = [ Active::class, City::class ]; . . .
Every thing is ready. just use it in your queries:
User::filter()->get();
if you want to specify some filter you can pass them thought this method:
User::filterThrough([City::class])->get();
It's good to mention that this package works with query string. Ex:
Route::get('/?country=germany', function() { return User->filter()->get(); });
So you should pass params through the url. The default query name is filter class name. Of course you can change the filters query name by:
class CountryFilter extends FilterContract { public static $queryName = 'country'; . . .
Also, you can set rules on your query string parameters:
class Active extends FilterContract { public function rules() { return [ 'required', Rule::in(['true', 'false']), ]; }
More than that sometimes we would like cast query string value. So:
class Active extends FilterContract { protected $cast = 'boolean'; public function rules() { return [ // ]; }
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email i.jedari@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
mjedari/larafilter 适用场景与选型建议
mjedari/larafilter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「query-filter」 「laravel-filter」 「mjedari」 「larafilter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mjedari/larafilter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mjedari/larafilter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mjedari/larafilter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.
Apply filters to Laravel's query builder.
The Eloquent Filter is a package for filter data of models by the query string. Easy to use and fully dynamic.
A clean, convention-based way to extract Eloquent query filters into dedicated filter classes — keep your controllers and scopes thin.
Eloquent model filtering based on URL queries
Laravel Eloquent models filter based on query parameters.
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-16
