定制 trocho/laravel-eloquent-filter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

trocho/laravel-eloquent-filter

Composer 安装命令:

composer require trocho/laravel-eloquent-filter

包简介

Allows filtering Eloquent queries by input filters

README 文档

README

This module lets you filter Eloquent data using query filters. You can apply WHERE filters and also set order of results

Installation

  1. Run

    composer require mnabialek/laravel-eloquent-filter

    in console to install this module

  2. That's it! Installation is complete. You don't need to adjust any config or install service providers.

Getting started

Let's assume you want to allow to filter users data. By default you use User Eloquent model to get users. To enable filtering, open User model (by default app/User.php file) and add into it the following trait:

use Mnabialek\LaravelEloquentFilter\Traits\Filterable;

just bellow opening class definition, so it should look something like this:

class User extends Authenticatable
{
    use Mnabialek\LaravelEloquentFilter\Traits\Filterable;

To allow filtering for this class, you need to also create implementation of Mnabialek\LaravelEloquentFilter\Contracts\QueryFilter interface. To do that create min app/Filters directory file with the following content:

<?php

namespace App\Filters;

use Mnabialek\LaravelEloquentFilter\Filters\SimpleQueryFilter;

class UserFilter extends SimpleQueryFilter
{
    protected $simpleFilters = ['id','created_at'];
    
    protected $simpleSorts = ['id','email','created_at'];
}

As you see, you don't implement here the whole contract but you only extend SimpleQueryFilter class that does it for you.

Now you need to go to place where you get users data. Let's assume in your controller you get your users using:

$users = User::get();

All you need to do now, is changing it into:

$users = User::filtered(\App::make(\App\Filters\UserFilter::class))->get();

Obviously you can use method dependency injection for that instead of using App::make here but it's only simple example that should work in all places of your app.

Now, let's assume you display your users using http://localhost/users url.

After those changes running:

  • http://localhost/users?id=2 - should display user with id = 2 only
  • http://localhost/users?id[]=2&id=5 - should display user with id = 2 and user with id = 5
  • http://localhost/users?sort=created_at,-email - should display users with created_at ascending and email descending order.

Simple query parser and simple query filter

Although you can create custom parsers and filters, some default ones are provided.

By default you can pass to your url conditions using field with value for example id=5 and you can apply sorting using sort parameter with names of fields separated by comma. If you precede field by - sign, it will assume you want to sort by this field in descending order.

When implementing your filter class when you extend SimpleQueryParser in $simpleFilters and $simpleSorts you can specify any fields that might be filtered and sorted without any custom implementation. For those fields simple = comparison will be used and in case of array usage (for example id[]=2&id[]=5 it will be assumed you want to get data with logical OR operator.

However in real life you might want to specify custom filter or sort method. To do that, you need to implement your custom method for such field for example:

For created_at filter you can use

protected function applyCreatedAt($value)
{
    $this->query->whereRaw('DATE(created_at) = ? ', [$value]);
}

For id sort you can use for example:

protected function applySortId($order)
{
   $this->query->orderBy('id',$order)->orderBy('email','asc');
}

You can also implement default filters and default sorting implementing applyDefaultFilters and applyDefaultSorts methods where you can check whether any filters or sorts were already applied.

Customization

Although this module provides some default implementations, you can create your own. You can change they way data is passed to QueryFilter. By default SimpleQueryParser is used that parses Request input in very basic way. However you might want to create your own implementation of Mnabialek\LaravelEloquentFilter\Contracts\InputParser to fully adjust it to your needs.

You might also change they way filters and sorts are applied to query. To do this, you need to create your own implementation of Mnabialek\LaravelEloquentFilter\Contracts\QueryFilter but again default implementation was given.

In case you want to only create implementation of QueryFilter, it might be convenient to create custom filter class, that will pass this specific QueryFilter implementation to QueryFilter instead of creating your own constructor in multiple filters classes. You can look at SimpleQueryFilter which does it for SimpleQueryParser to remove need of defining this constructor over and over in multiple filter classes (assuming you want to use SimpleQueryParser for them).

Licence

This package is licenced under the MIT license

trocho/laravel-eloquent-filter 适用场景与选型建议

trocho/laravel-eloquent-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「filter」 「request」 「input」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 trocho/laravel-eloquent-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 trocho/laravel-eloquent-filter 我们能提供哪些服务?
定制开发 / 二次开发

基于 trocho/laravel-eloquent-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-21