flowcontrol/query-filters
Composer 安装命令:
composer require flowcontrol/query-filters
包简介
Query filters for an Eloquent model
README 文档
README
Inspired by Laracasts and Jeffrey Way - https://github.com/laracasts/Dedicated-Query-String-Filtering/
Install using Composer
composer require flowcontrol/query-filters
And add the ServiceProvider - FlowControl\QueryFilters\QueryFiltersServiceProvider::class
Usage
Define a QueryFilter class (you can run the php artisan flowcontrol:query-filter filterName command to generate the scaffold):
<?php
namespace App\QueryFilters;
use Illuminate\Database\Eloquent\Builder;
use FlowControl\QueryFilters;
class LessonFilters extends Filter
{
/**
* @param string $order
* @return Builder
*/
public function popular($order = 'desc')
{
return $this->builder->orderBy('views', $order);
}
/**
* @param string $level
* @return Builder
*/
public function difficulty($level)
{
return $this->builder->where('difficulty', $level);
}
}
Make your model use the FlowControl\QueryFilters\Filterable trait.
And lastly in your controller or where you want this logic to run:
<?php
namespace App\Http\Controllers;
use App\Models\Lesson;
use App\QueryFilters\LessonFilters;
class LessonsController extends Controller
{
public function index(LessonFilters $filters)
{
return Lesson::filter($filters)->get();
}
}
ListView sorting
By default the base Filter class will handle the sorting for your main table. If you have a ListView that uses a column which is a relationship to the model, you'll have to handle that column for yourself. Let's imagine that you have User model and it has a relationship to UserType (one User has one UserType). So in your ListView you want to sort by the column that shows the name of the UserType. We'll assume that the column in the ListView is defined as type.name. In that case you need to define the custom sort like this:
public function sortTypeName($dir)
{
return $this
->builder
->join('user_types', 'user_types.id', '=', 'users.type_id')
->orderBy('user_types.id', $dir);
}
flowcontrol/query-filters 适用场景与选型建议
flowcontrol/query-filters 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 532 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 flowcontrol/query-filters 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flowcontrol/query-filters 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 532
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-28