infinitypaul/laravel-database-filter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

infinitypaul/laravel-database-filter

Composer 安装命令:

composer require infinitypaul/laravel-database-filter

包简介

Need to filter database results with a query string? Here's a beautiful, easy to extend laravel package to keep your code super tidy.

README 文档

README

Latest Version on Packagist Build Status Quality Score Total Downloads

Ever been stuck with filtering a database table with lots of GET parameters? I can only imagine how bulky your code will be. Relax, the easiest solution to this problem is right here. Let's say we have to query our records table with the following get parameters

https://www.example.com/records?dept=csc&level=200&grade=A

Installation

You can install the package via composer:

composer require infinitypaul/laravel-database-filter

The package will automatically register itself, but if your laravel versions is < 5.5 you will need to add Infinitypaul\LaravelDatabaseFilter\LaravelDatabaseFilterServiceProvider::class, service provider under your config/app.php file.

Usage

Once the package is installed, an artisan command is available to you.

php artisan make:filter 

We would be working with the records table below:

Id Department Level Score Grade
1 csc 200 68 b
2 physics 100 90 a
3 csc 100 90 a
4 physics 200 60 b
5 csc 200 80 a

The Filtering be done in 6 simple steps. Perfect right! Let's begin:

* Step 1

Create a mother filter class, this is where all filters will be recorded. This can be created with this one line of code:

php artisan make:filter RecordFilter --model

This package will generate a new PHP file RecordFilter.php under app/Filters folder. This is where all other filters will be created.. It wil look like this

<?php

namespace App\Filters;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FiltersAbstract;

class RecordFilter extends FiltersAbstract {
        protected $filters = [];
}

* Step 2:

Since we are working with the records table, we will be working on the Record model. So open your Record model and include the following: * The Filter Trait * The Record Filter Class

namespace App;

use App\Filters\RecordFilter;
use Infinitypaul\LaravelDatabaseFilter\Traits\filterTrait;


class Course extends Model
{
    use filterTrait;

    protected $filter = RecordFilter::class; //mother filter class

}

* Step 3:

We would be filtering with 3 parameters (dept, level, grade):

Let's create filter classes for each filter

php artisan make:filter DeptFilter
php artisan make:filter LevelFilter
php artisan make:filter GradeFilter

The above commands will also generate a new PHP file under app/Filters folder with the name DebtFilter.php, LevelFilter.php and GradeFilter.php which will look like below.

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

class DebtFilter extends FilterAbstract
{
    
        public function mappings ()
        {
            return [];
        }

        
        public function filter(Builder $builder, $value)
        {
            return $builder;
        }
}

Well Done!!! Let's move to the next step

* Step 4:

Now we write our logic in each class:

In our DeptFilter class, we write the following:

  namespace App\Filters;

            use Illuminate\Database\Eloquent\Builder;

            use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

            class DeptFilter extends FilterAbstract
            {

                    public function mappings ()
                    {
                        return [];
                    }

                    public function filter(Builder $builder, $value)
                    {
                        return $builder->where('dept', $value);
                    }
            }

In our LevelFilter class, we write the following:

            namespace App\Filters;

            use Illuminate\Database\Eloquent\Builder;

            use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

            class LevelFilter extends FilterAbstract
            {

                    public function mappings ()
                    {
                        return [];
                    }

                    public function filter(Builder $builder, $value)
                    {
                        return $builder->where('level', $value);
                    }
            }

In our GradeFilter class, we write the following:

            namespace App\Filters;

            use Illuminate\Database\Eloquent\Builder;

            use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

            class GradeFilter extends FilterAbstract
            {

                    public function mappings ()
                    {
                        return [];
                    }

                    public function filter(Builder $builder, $value)
                    {
                        return $builder->where('grade', $value);
                    }
            }

The filter method takes in our conditions or whatever checks against the database

Bravo on completing that. Moving on!!!

* Step 5:

We write our search parameters against the filter class. Let's open up our mother filter class - RecordFilter to register all the conditions we have generated.

            namespace App\Filters;

            use Infinitypaul\LaravelDatabaseFilter\Abstracts\FiltersAbstract;

            class RecordFilter extends FiltersAbstract {
                    protected $filters = [
                        'dept' => DeptFilter::class,
                        'level' => LevelFilter::class,
                        'grade' => GradeFilter::class
                    ];
            }

The $Filter array key is will be the query params.

* Step 6:

Assuming we have a controller RecordController, we can create a function to get our record:

namespace App\Http\Controllers;

use App\Course;
use App\Filters\AccessFilter;
use Illuminate\Http\Request;

class RecordController extends Controller
{
    public function index(Request $request){
        return Course::filter($request)->get();
    }
}

We are all done!!!

When we enter the following into our browser

https://www.example.com/records?dept=csc&level=200&grade=a

We will get the following result:


            {
                "data": [
                    {
                        "id": 5,
                        "department": "csc",
                        "level": 200,
                        "score": "80",
                        "grade": "a"
                    }
                ]
            }

Tweak Time

To be strict about query params input, we can use the mapping method or leave empty for free entry.

Let's say when returning the data, we want all department with csc to return as CSC. We return values to the mappings function in the DeptFilter class as shown below:

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

use Infinitypaul\LaravelDatabaseFilter\Abstracts\FilterAbstract;

class DeptFilter extends FilterAbstract
{
    
        public function mappings ()
        {
            return [
                'csc' => 'CSC'
            ];
        }

public function filter(Builder $builder, $value)
                    {
                        return $builder->where('dept', $value);
                    }

With the above setup, once csc is entered in your query params, it will return CSC as value.

Lastly, You can add local scoped filter() by passing an array of filter into the filter scope.

  public function index(Request $request){
        return Record::filter($request, ['score' => DifficultyFilter::class])->get();
    }

You can also override your filter property from your controller in this manner

  public function course(Request $request){
        return Record::filter($request, PaulFilter::class, ['score' => DifficultyFilter::class])->get();
    }

So instead of your filter to use the RecordFilter it will make use of PaulFilter

Bug & Features

If you have spotted any bugs, or would like to request additional features from the library, please file an issue via the Issue Tracker on the project's Github page: https://github.com/infinitypaul/laravel-database-filter/issues.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email infinitypaul@live.com instead of using the issue tracker.

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!

Don't forget to follow me on twitter!

Thanks! Edward Paul.

License

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

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

infinitypaul/laravel-database-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47.42k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2020 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 47.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-01