duuany/eloquent-filters
Composer 安装命令:
composer require duuany/eloquent-filters
包简介
A simplest Eloquent query filter package.
README 文档
README
A simplest Eloquent Model URL query filter package for your Laravel
Instalation
You can install via composer:
Laravel 5.5+
$ composer require duuany/eloquent-filters
###Laravel 5.4 or above
$ composer require duuany/eloquent-filters:1.1
Add the service provider to your config/app.php
Duuany\EloquentFilters\EloquentFiltersServiceProvider::class
Optionally, you can publish config file to override package configuration
$ php artisan vendor:publish --provider="Duuany\EloquentFilters\EloquentFiltersServiceProvider" --tag="config"
Usage
In your model, add the following HasFilters trait
class User extends Model { use HasFilters; }
Create a UserFilter class anywhere in your app folder. Filter classes defines array of applicable filters. For each filter added to array of filters, you need to implement the filter logic.
You can create filters via artisan command
$ php artisan make:filter FilterName
class UserFilter extends EloquentFilters { protected $filters = [ 'order', 'popular', .... ]; protected function order($column) { return $this->builder->orderBy($column, 'desc'); } }
You can pass multiple parameters to your filters, like this:
protected function order($column, $sort = 'desc') { return $this->builder->orderBy($column, $sort); }
When passing more than one paramenter to filter, make sure use a delimiter in your query string.
http://myurl.dev?order=id:asc
The default delimiter its :, but your can modify overriding the protected $delimiter property.
Example:
class UserFilter extends EloquentFilters { protected $delimiter = '|'; ... }
In query string...
http://myurl.dev?order=id|asc
... keep in mind, to not use special query strings characters like delimiters.
Magic Calls
Optionally, you can ommit ->builder:
// this... return $this->builder->orderBy($column, $sort); // can be called like this... return $this->orderBy($column, $sort);
Now in your application you can use the filters as the following:
class UserController extends Controller { public function index(UserFilter $filters) { $users = User::filter($filters)->get(); } }
When the user access the URL http://localhost/?order=id, the order filter will be applied on the User model.
Testing
$ ./vendor/bin/phpunit
Credits
- Jeffrey Way
- Jorge Junior aka jjquady
- Many coffee cups :D
License
The MIT License (MIT). Please see License File for more information.
duuany/eloquent-filters 适用场景与选型建议
duuany/eloquent-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 588 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 duuany/eloquent-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 duuany/eloquent-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 588
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-08