mayoz/eloquent-filterable
Composer 安装命令:
composer require mayoz/eloquent-filterable
包简介
Query filter for your Eloquent models.
README 文档
README
With this package, you can optimize query clauses calling before or after the filter. You can manage your queries from a single interface.
Installation
PHP 5.5.9+ or HHVM, and Composer are required.
To get the latest version of Eloquent Filterable, simply add the following line to the require block of your composer.json file:
"require": { "mayoz/eloquent-filterable": "~2.0" }
You'll then need to run composer install or composer update to download it and have the autoloader updated. Or use to shortcut installed through terminal:
composer require mayoz/eloquent-filterable ~2.0
Usage
- Create your query filters.
- Create your Eloquent models.
- Define
Filterabletrait and use the query filters in the Eloquent model.
Create Filters
All filters should be extend Mayoz\Filter\Filter abstract class. Thus, can be used before and after methods in your filters.
The Before Method
The before method responsible to position the head of the WHERE clause of the query. For example; we need published = 1 of query WHERE clause. However, this clause would like to work on before the other clauses.
<?php namespace App\Filters; use Mayoz\Filter\Filter; class PublishedFilter extends Filter { /** * The first executable filter clause. * * @param mixed $query * @return void */ public function before($query) { $query->where('published', '=', 1); } }
The After Method
The after method responsible to position the end of the WHERE clause of the query. For example, if need status = 'active' of query WHERE clause. However, this clause would like to work on after the other clauses.
<?php namespace App\Filters; use Mayoz\Filter\Filter; class StatusActiveFilter extends Filter { /** * The last executable filter clause. * * @param mixed $query * @return void */ public function after($query) { $query->where('status', '=', 'active'); } }
Create Model
Create your model file. If you want to manage queries add the Filterable trait your model file. And than, assign the all associative filters to $filters variable. Consider the following example:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Mayoz\Filter\Filterable; class Post extends Model { use Filterable; /** * The attributes that should be filter. * * @var array */ protected $filters = [ 'App\Filters\StatusActiveFilter', 'App\Filters\PublishedFilter' ]; // other things... }
Important: The order of the filter is important when adding (if need multiple before or after filters) query clause. The before filters are added the head of the clause according to the reference sequence. Likewise, the after filters.
Debug
Now the Post model is ready to use. You ready? Okay, we're testing the query. Don't forget to enable the query logging for examine the model queries.
$model = \App\Post::where('views', '>', 100)->get();
Query logging output:
SELECT * FROM `posts` WHERE `published` = 1 # added by automatic filter. AND `views` > 100 # user defined where clause AND `status` = 'active' # added by automatic filter.
Why Use?
For example, you might want to show only the approved text of the visitors on the site. However, administrators can see them all. Create a new extended filter:
<?php namespace App\Filters; use Auth; use Mayoz\Filter\Filter; class StatusActiveFilter extends Filter { /** * The last executable filter clause. * * @param mixed $query * @return void */ public function after($query) { # assume, the `users` table has `role` field. if (array_get(Auth::user(), 'role') != 'administrator') { $query->where('status', '=', 'active'); } } }
Hocus, pocus! Visitors will display only the active posts. But administrator display all. Ok?
Contributing
Love innovation and simplicity. Please use issues all errors or suggestions reporting. Follow the steps for contributing:
- Fork the repo.
- Follow the Laravel Coding Style.
- If necessary, check your changes with unittest.
- Commit all changes.
- Push your commit and create a new PR.
- Wait for merge the PR.
Unit Test
Please create your tests and check before PR. Use the command:
$ phpunit
License
Eloquent Filterable is licensed under The MIT License (MIT).
mayoz/eloquent-filterable 适用场景与选型建议
mayoz/eloquent-filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 117 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mayoz/eloquent-filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mayoz/eloquent-filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mayoz/eloquent-filterable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Symfony DataGridBundle
统计信息
- 总下载量: 117
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-28