milito/query-filter
Composer 安装命令:
composer require milito/query-filter
包简介
This is package to use query filter for each model. This package is made entirely for personal use. But if you are eager to use this, you can easily use this package by using readme. This is very simple and amateur :).
README 文档
README
Laravel Query filter package
A simple package for adding your query filter files to project.
Introduction
Install the package with composer using the following command:
composer require milito/query-filter
Usage
For create new query filter you can run this command in your terminal:
php artisan make:query-filter {your-query-filter-name}
Example:
php artisan make:query-filter ProductsFilter
This command will create a ProductsFilter.php file in app/Filters/ path.
This ProductsFilter class, is extended from QueryFilter class, and it used for controller functions.
And because of that, constructor of this class requires a request object.
But if you want to create a filter class with a simple array input, you need to
use -a|--array option after your class name.
php artisan make:query-filter ProductsFilter -a
This ProductsFilter class, is extended from ArrayQueryFilter.
Namespace
You can add namespace to your file:
php artisan make:query-filter Products/ProductsFilter
The above will create a Products directory inside the app/Filters directory.
How to use!
After file generated you should add your functions.
Example:
<?php namespace App\Filters; use Milito\QueryFilter; class ProductsFilter extends QueryFilter { public function name($name = null) { if (!$name) return $this->builder; return $this->builder->where('name' , 'LIKE' , $name); } }
HINT: If your function parameters don't have default values, you need to fill with value every time you want to use it.
Then you need to add QueryFilterScope to your model.
Example :
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Milito\QueryFilterScope; class Product extends Model{ use QueryFilterScope; }
Now you can use in your controller :
<?php namespace App\Controllers; use App\Filters\ProductsFilter; use App\Models\Product; class ProductsController extends Controller { public function index(ProductsFilter $filter) { $products = Product::filter($filter)->paginate(20); return $products; } }
or :
<?php namespace App\Controllers; use App\Filters\ProductsFilter; use App\Models\Product; use Illuminate\Http\Request; class ProductsController extends Controller { public function index(Request $request) { $filter = new ProductsFilter($request); $products = Product::filter($filter)->paginate(20); return $products; } }
You can use request() function to get request from filter if you need request.
Example:
//... public function index(ProductsFilter $filter) { $request = $filter->request(); $products = Product::filter($filter)->paginate($request->per_page??20); return $products; } //...
To use a class that inherits from ArrayQueryFilter:
//... public function index(Request $request) { $filter = new ProductsFilter(["name" => $request->query("your_name_field")]); $products = Product::filter($filter)->paginate(20); return $products; } //...
License
The Milito Query Filter package is open-sourced package licensed under the MIT license.
milito/query-filter 适用场景与选型建议
milito/query-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.97k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「query」 「request」 「builder」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 milito/query-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 milito/query-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 milito/query-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nova searchable filter for belongsTo relationships.
Query filtering in your frontend
Anax Database Active Record module for model classes.
Symfony DataGridBundle
HTTP request logger middleware for Laravel
Adds request-parameter validation to the SLIM 3.x PHP framework
统计信息
- 总下载量: 8.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-06