behamin/bfilters
Composer 安装命令:
composer require behamin/bfilters
包简介
A package for customized filtering on eloquent models
README 文档
README
apply auto filters in Eloquent.
with this package, easily apply filters, sort and paginatation on Eloquent models and their relations.
with query string parameters.
Installation
composer require behamin/bfilters
Updating your Eloquent Models
Your models should use the HasFilter trait:
use BFilters\Traits;
class MyModel extends Eloquent
{
use HasFilter;
// define fields for full text serach as "searchable" or "fillable"
protected $searchable = [
'first_name',
'last_name'
];
}
Create Filter Class
php artisan make:filter {name}
for example:
php artisan make:filter UserFilter
In Created Filter Class
use Illuminate\Http\Request;
class UserFilter extends Filter
{
public function __construct(Request $request)
{
parent::__construct($request);
$this->relations = [
//actual name of relation defined in original model
'relationName1' => [
'searchName1' => 'Original column1 Name in Relation table',
'searchName2' => 'Original column2 Name in Relation table',
'searchName3' => 'Original column3 Name in Relation table',
//if searchName and original column name is same
'Original column4 Name in Relation table'
],
'posts' => [
// in this case when you set posted_at as your filter the filter will applied on 'created_at' field of original table
'posted_at' => 'created_at',
'title',
'topic',
],
];
// you set this variabe if you want to have sum of your entries based of a specific field (f.e id here)
$this->sumField = 'id';
// define valid eager loading relationships to protect loading unwanted data
$this->validWiths = ['comments', 'tags'];
}
}
Usage
In controllers
public function index(YourModelFilter $filters): Response
{
[$entries, $count, $sum] = YourModel::filter($filters);
}
In Request
filter:{
"sort":[
{ "field": "created_at", "dir": "asc" },
{ "field": "first_name", "dir": "desc" }
],
"page":{ "limit": 10, "offset": 0 },
"filters":[
//(first_name LIKE '%alireza%' or last_name = '%bahram%') and (mobile LIKE '%9891%')
[
//use "or" for fields in same array & use "and" for fields in different array
{"field": "first_name", "op": "like", "value": "alireza"},
{"field": "last_name", "op": "=", "value": "bahram"}
],
[
{"field": "mobile", "op": "like", "value": "9891"}
],
[
//full search : search a string in fields you set in its model "searchable" or "fillable" arrays
{"value" : "al"}
]
],
"with":[
"comments",
"tags"
]
}
Query String Samples:
pagination per_page=10 page=0 :
?filter={"page":{"limit": 10,"offset": 0}}
pagination per_page=20 and page=0 (sort_by id desc) :
?filter=%7B%22page%22:%7B%22limit%22:20,%22offset%22:0%7D,%22sort%22:[%7B%22field%22:%22id%22,%22dir%22:%22desc%22%7D]%7D
like before field comment like aaaaaaaaa :
?filter=%7B%22page%22:%7B%22limit%22:20,%22offset%22:0%7D,%22sort%22:[%7B%22field%22:%22id%22,%22dir%22:%22desc%22%7D],%22filters%22:[[%7B%22field%22:%22comment%22,%22op%22:%22like%22,%22value%22:%22aaaaaaaa%22%7D]]%7D
behamin/bfilters 适用场景与选型建议
behamin/bfilters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.69k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 behamin/bfilters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 behamin/bfilters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-31