定制 acodingproject/laravel-filterable 二次开发

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

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

acodingproject/laravel-filterable

Composer 安装命令:

composer require acodingproject/laravel-filterable

包简介

Using URL query strings to filter Eloquent queries.

README 文档

README

Laravel Filterable

Latest Version on Packagist Build Status Total Downloads

This package allows you to easily handle database filtering through query strings. The idea is taken from one of the Jeffrey's videos (behind the paywall). One quick example might look like this: /users?filter-username=~joe will result in SQL query select * from users where "username" like '%joe%'.

Installation

You can install the package via composer:

composer require kyslik/laravel-filterable

Laravel will discover the package by itself. If you feel old-school, disable auto-discovery and add Kyslik\LaravelFilterable\FilterableServiceProvider::class to the providers array in your config/app.php.

Before we start

Package lets us create & apply two kinds of filters: custom and generic.

Custom filters

Custom filters are just like in Jeffrey's video. We define our logic on builder instance and package will apply it via local scope.
Let's say we want to display recently created records. We create method recent($minutes = null) inside our filter class, this method returns Builder instance:

public function recent($minutes = null)
{
    $minutes = (is_numeric($minutes)) ? $minutes : 30;

    return $this->builder->where('created_at', '>=', Carbon\Carbon::now()->subMinutes($minutes));
}

Full example is shown later on.

Generic filters

Generic filters are those defined in config file. By default, the package supports filtering timestamps, ranges, ins, booleans and strings.

/?filter-created_at=t>=1510952444
/?filter-id=><1,19
/?filter-id=i=1,5,10,12
/?filter-admin=b=yes
/?filter-username=joe
/?filter-username=~joe
/?filter-username=~joe&filter-admin=b=yes&filter-created_at=t=1510952444

Default operator matrix for generic filters

operator accepts description
= string equal
!= string not equal
> string greater than
< string less than
>= string equal or greater than
<= string equal or less than
~ string like
!~ string not like
>< comma separated list between
!>< comma separated list not between
i= comma separated list in
i!= comma separated list not in
b= 1, 0, true, false, yes, no equal
b!= 1, 0, true, false, yes, no not equal
t= UNIX timestamp equal
t!= UNIX timestamp not equal
t> UNIX timestamp greater than
t< UNIX timestamp less than
t>= UNIX timestamp equal or greater than
t<= UNIX timestamp equal or less than
t>< UNIX timestamp between
t!>< UNIX timestamp not between

Usage

While using both custom or generic filters we must:

  1. have local scope on model with signature scopeFilter(Builder $query, FILTERNAME $filters)
  2. have particular (FILTERNAME) filter class that extends one of:
    • Kyslik\LaravelFilterable\GenericFilterable class - enables us to use both custom & generic filters
    • Kyslik\LaravelFilterable\Filterable class - enables us to use only custom filters
  3. call scope within a controller

Example with custom filters

Let's say we want to use filterable on user model. We will have to create filter class App/Filters/UserFilter.php, specify filterMap() and method with our custom logic.

<?php
namespace App\Filters;

use Kyslik\LaravelFilterable\Filterable;

class UserFilters extends Filterable
{
    public function filterMap()
    {
        return ['recent' => ['recently', 'recent']];
    }
	
    public function recent($minutes = null)
    {
        $minutes = (is_numeric($minutes)) ? $minutes : 30;

        return $this->builder->where('created_at', '>=', \Carbon\Carbon::now()->subMinutes($minutes)->toDateTimeString());
    }
}

Note: filterMap() must be defined and it should return associative array where key is method name and value is either alias or array of aliases

Secondly, we will have to add a local scope to our user model via trait:

use Kyslik\LaravelFilterable\FilterableTrait;

...
class User extends Model 
{
    use FilterableTrait;
    ...
}

Finally, in user controller we will have to call model's scope:

public function index(User $user, UserFilters $filters) 
{
    return $user->filter($filters)->paginate();
}

Now we can visit users?recent or users?recently or users?recent=25 and results will be filtered by recent() method defined in UserFilters class.

Example with generic filters

Let's say we want to use filterable on user model. We will have to create filter class App/Filters/UserFilter.php and specify $filterables.

<?php
namespace App\Filters;

use Kyslik\LaravelFilterable\GenericFilterable;

class UserFilters extends GenericFilterable
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
}

Secondly, we will have to add a local scope to our user model via trait:

use Kyslik\LaravelFilterable\FilterableTrait;

...
class User extends Model 
{
    use FilterableTrait;
    ...
}

Finally, in our user controller we will have to call model's scope:

public function index(User $user, UserFilters $filters) 
{
	return $user->filter($filters)->paginate();
}

We are ready to filter user model.

Note: behind the scenes GenericFilterable class extends Filterable class, thus using GenericFilterable also enables us to apply custom filters defined within the filter class

Additional configuration

While using generic filters we may define which generics should be allowed. In our filter class we define settings() method in which we specify settings.

...
class UserFilters extends GenericFilterable
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
    
    protected function settings()
    {
        // global settings for this filter, pick either "except" or "only" logic
        $this->only(['=', '~', '!~']);
        // $this->except(['!=']);

        // settings applied only to some columns, these settings ignore settings above
        $this->for(['username', 'id'])->only(['!=', '>=', '=', '~']);
        $this->for(['id'])->only(['=', '!=', '~']); //settings for "id" will be re-written
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email martin.kiesel@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

acodingproject/laravel-filterable 适用场景与选型建议

acodingproject/laravel-filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 805 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-24