marksihor / laravel-filtering
Laravel 模型过滤
2.994
2023-12-06 12:58 UTC
Requires
- php: >=7.1
- laravel/framework: *
This package is auto-updated.
Last update: 2024-09-06 14:27:06 UTC
README
该包允许您通过用户请求在1行中制作模型过滤,您只需在Controller.php(或您需要的任何控制器)中使用MarksIhor\LaravelFiltering\Filterable特性。
自2.00版本起,支持按模型关系进行过滤。
安装
注意:建议使用新的过滤包:[https://packagist.org.cn/packages/marksihor/laravel-query-filter](https://packagist.org.cn/packages/marksihor/laravel-query-filter)
$ composer require marksihor/laravel-filtering -vvv
使用
在您的控制器中使用Filterable特性
<?php namespace App\Http\Controllers; use MarksIhor\LaravelFiltering\Filterable; class Controller { use Filterable; }
使用示例(在控制器中)
$collection = $this->filter(Model::query())->get(); // give user ability to filter model without any constraints $collection = $this->filter(Model::where('price', 100))->get(); // predefined filter that user cannot override in request parameters $collection = $this->rawParams(['status' => 'paid'])->filter(Model::query())->get(); // also a way to predefine parameters that user cannot override $collection = $this->filterable(['status','paid'])->filter(Model::query())->get(); // allows you to define columns that user can filter
使用示例(在模型中)
public static $filterable = []; // to define columns that you will be able to filter (not required) public static $filterableRelations = []; // to define relations that you will be able to filter (otherwise it won't work) public static $filterablePivot = [ 'role_id' => 'sites', ]; // key = key, value = relation name (to filter by pivot table columns, otherwise it won't work) public static $morphSubRelations = [ 'vault' => ['billingAddress'] ]; // to filter through morph relation, need to specify morph relation sub relations
使用示例(在URL中)
https://yoursite.com/path?status=active https://yoursite.com/path?price=100,75 https://yoursite.com/path?price=100,75&status=paid https://yoursite.com/path?price[from]=100&price[to]=500 https://yoursite.com/path?price[orderBy]=asc https://yoursite.com/path?with=metas,tags https://yoursite.com/path?id[not_in]=1,3,5 https://yoursite.com/path?id[in]=1,3,5 https://yoursite.com/path?data->key=value // to filter json column https://yoursite.com/path?data__key=value // to filter json column https://yoursite.com/path?users[id]=1 // to filter relationships https://yoursite.com/path?select[accounts]=id,user_id,link // to select column from related table when use ?with=accounts, note that the relational column should be in the list https://yoursite.com/path?select[current_model_table_name]=id,name // to select column from curent model https://yoursite.com/path?deleted=1 // get only softDeleted records https://yoursite.com/path?withCount=relationName // get count of specified relation https://yoursite.com/path?withCount[posts][quantity]=2&withCount[posts][status]=completed // get count of specified relation with filters https://yoursite.com/path?has=relationName // get only records that has specified relations (1 or more) https://yoursite.com/path?has=relationName.relationOfRelationName // get only records that has specified relations (1 or more) https://yoursite.com/path?has[posts][quantity]=2&withCount[posts][status]=completed // get only records that has specified relations with filters https://yoursite.com/path?hasNot=relationName // get only records that hasNot https://yoursite.com/path?hasNot[posts][quantity]=2 // get only records that hasNot specified relations with filters https://yoursite.com/path?column1=null&column2=notNull // for null and not null values https://yoursite.com/path?tags[name]=tagname // to filter by relationship, the model should have public static $filterableRelations = ['tags']; https://yoursite.com/path?order=asc&orderBy=column // new ordering way https://yoursite.com/path?with=category.siteCategory&category[siteCategory.id]=10&category[id]=7 // filter by relation on relation https://yoursite.com/path?whereDoesntHave[categories][id]=2 // filter by relation https://yoursite.com/path?order=rundom // order by rundom
如您所见,您可以组合请求中的参数(参数顺序不重要)。
发行说明
2.00 - 模型关系过滤。
许可证
MIT