承接 timedoor/laravel-filter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

timedoor/laravel-filter

Composer 安装命令:

composer require timedoor/laravel-filter

包简介

Package for filtering data from request

README 文档

README

This package is used to filter data from request in a simple way.

Installation

You can install the package via composer:

composer require timedoor/laravel-filter

Usage

Create a filter class

php artisan make:filter UserFilter

In default, all filter class are stored inside app/Http/Filters folder. If you want to store it in another folder, you can pass the full namespace instead of the name

php artisan make:filter App/Foo/Bar/YourFilter

After you successfully create the filter class, it will look like this:

<?php

namespace App\Filters;

class UserFilter
{
    public function keyword($builder, $value)
    {
        //
    }
}

Use Filterable trait

Add the Filterable trait inside your model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Timedoor\LaravelFilter\Concerns\Filterable;

class User extends Model
{
    use Filterable; 
}

Filtering Data

After you finished the required setup, now you can filter data in your controller:

use App\Http\Filters\UserFilter;

$users = User::applyFilter(UserFilter::class)->get();

Cases

Let's say your application inside http://your-domain.com and you want to filter user data by their name, so the request will be like http://your-domain.com?name=John you can handle it like this:

// app/Http/Filters/UserFilter.php

<?php

namespace App\Filters;

class UserFilter
{
    public function name($builder, $value)
    {
        return $builder->where('name', 'LIKE', "%{$value}%");
    }
}
// app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;

class HomeController extends Controller
{
    public function index()
    {
        $users = User::applyFilter(UserFilter::class)->get()
        
        return $users;
    }
}

Advance Usage

Using Custom Laravel Query Builder

In case you want to use your own laravel query builder, you need to follow this setup:

// app/QueryBuilders/UserQueryBuilder.php

<?php

namespace App\QueryBuilders;

use Timedoor\LaravelFilter\LaravelFilterQueryBuilder;

class UserQueryBuilder extends LaravelFilterQueryBuilder
{

}

Make sure your query builder is extended to LaravelFilterQueryBuilder class

// app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Timedoor\LaravelFilter\Concerns\Filterable;
use App\QueryBuilders\UserQueryBuilder;

class User extends Model
{
    use Filterable; 
    
    /**
     * Create a new Eloquent query builder for the model.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @return \App\QueryBuilders\UserQueryBuilder
     */
    public function newEloquentBuilder($query)
    {
        return new UserQueryBuilder($query);
    }

    /**
     * @param  stdClass  $subject
     * @param  \Illuminate\Http\Request|null  $request
     * @param  array<string, mixed>  $options
     * @return \App\QueryBuilders\UserQueryBuilder
     */
    public static function applyFilter($subject, $options = [], Request $request = null)
    {
        /** @var \App\QueryBuilders\UserQueryBuilder $builder */
        $builder = (new static)->newQuery();

        return $builder->applyFilter($subject, $options, $request);
    }
}

Now you can implement filter with your own query builder.

Using with Options

There are 2 options that available include and exclude you can pass the options inside applyFilter() method

User::applyFilter(UserFilter::class, [
    'include' => [
        'name' => 'John',
        'email' => 'fhuel@example.org'
    ],
    'exclude' => ['address']
])

Include

This option is when you want to call filter method even if the request params is not set. For example you want to filter user by their names and your endpoint looks like this http://localhost/user?name=John. But if the request doesn't have name query your filter method will not called. If you want to always call your filter method even if the request doesn't have query, you can use include option

User::applyFilter(UserFilter::class, [
    'include' => ['name' => 'John']
])

Exclude

This option is for prevent the filter method called. For example you have name() method inside your filter class and the request query looks like this http://localhost/user?name=John. But you don't want name() method called, you can use exclude option

User::applyFilter(UserFilter::class, [
    'exclude' => ['name']
])

Chaining Filter

If you want to use more than 1 filter class, you can chain the applyFilter method, so it will looks like this:

User::applyFilter(Foo::class)
    ->applyFilter(Bar::class)
    ->get()

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-03