kblais/query-filter
最新稳定版本:v3.3.0
Composer 安装命令:
composer require kblais/query-filter
包简介
Easily create filters for your Eloquent models.
README 文档
README
Easily create filters for your Eloquent model.
Based on Jeffray Way's Laracast tutorial.
Installation
You can install the package via composer:
composer require kblais/query-filter
You can publish the config file with:
php artisan vendor:publish --provider="Kblais\QueryFilter\QueryFilterServiceProvider" --tag="query-filter-config"
This is the contents of the published config file:
return [ 'default-filters-source' => null, ];
Usage
A QueryFilter is a class to apply, based on an array or a Request, multiple conditions.
You can call any Eloquent method directly from filter methods.
use Kblais\QueryFilter\QueryFilter; class PostFilter extends QueryFilter { public function title($value) { return $this->where('foo', 'bar'); } public function author($value) { return $this->whereHas('author', function ($builder) use ($value) { $this->where('name', $value); }); } }
To allow a model to use query filters, you have to add the Filterable trait on your model.
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Kblais\QueryFilter\Filterable; class Post extends Model { use Filterable; }
You can then use the filter() scope from anywhere:
// From an array... $filterInput = [ 'title' => 'Les Trois Mousquetaires', ]; $posts = Post::filter(PostFilter::make($filterInput))->get(); // ...Or in a controller action public function index(PostFilter $filter) { // Filter is automatically populated with Request data when injected return Post::filter($filter)->get(); }
If your filter parameters are always placed in an array key (for example filters), you can define the default-filters-source config key in the config file, or add a protected string $source = 'filters' in your QueryFilter.
Frequent issues
Call to an undefined method App\QueryFilters\YourFilter::anEloquentScope(). with PHPStan
To fix this error message, add the following DocBlock to your filter:
/**
* @mixin \App\Models\ModelOfYourScope
*/
Testing
The test suite is composed of 3 tests: PHPCsFixer (coding style), PHPStan (static analysis) and PHPUnit (unit tests).
You can run all these tests running the following command:
composer tests
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
- Follow the PSR-2 Coding Standard. Use PHP-CS-Fixer to apply the conventions.
- Add tests for the features you add and bugs you discover.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 44.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-10