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

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

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

baraadark/laravel-filter

Composer 安装命令:

composer require baraadark/laravel-filter

包简介

LaravelFilter is a package designed to simplify the process of filtering table fields in a Laravel project. It provides a straightforward way to implement custom query filters for your models.

README 文档

README

LaravelFilter is a package designed to simplify the process of filtering table fields in a Laravel project. It provides a straightforward way to implement custom query filters for your models.

Latest Version on Packagist Packagist License Total Downloads

Getting started

Installation

Please check the official laravel installation guide for server requirements before you start. Official Documentation

You can install the package via composer:

composer require baraadark/laravel-filter

Next, publish the configuration file:

php artisan vendor:publish --tag=config

Usage

Applying the Filterable Trait

Use the Filterable trait in your models to enable filtering.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BaraaDark\LaravelFilter\Traits\Filterable;

class YourModel extends Model
{
    use Filterable;
}

Override filterKeys function

You should override the filtersKeys method to return an array of filter keys and their corresponding filter classes.

Example:

use BaraaDark\LaravelFilter\Traits\Filterable;

class YourModel extends Model
{
    use Filterable;

    public function filtersKeys(): array
    {
        return [
            // 'filter-key' => FilterClass::class
        ];
    }
}

The filtersKeys method returns an associative array where the keys are the names of the filter keys expected from the request, and the values are the filter classes that contain the query logic.

Creating a Filter Class

To create a Filter class, run the command:

php artisan make:filter

You will be prompted to enter the class name and the related model name. The generated file will be located at App\Http\Filters\ModelName.

Filter Class Structure

namespace App\Http\Filters\ModelName;

use BaraaDark\LaravelFilter\Filter;

class FilterClass extends Filter
{
    /**
     * Get the validation rules that apply to the filter request.
     *
     * @return array
     */
    public static function rules(): array
    {
        return [];
    }

    /**
     * Apply filter query on the related model.
     *
     * @param \Illuminate\Database\Eloquent\Builder &$query
     */
    public function apply(&$query)
    {
        return $query;
    }
}

Example Filter Class:

use BaraaDark\LaravelFilter\Filter;

class ProductPriceRangeFilter extends Filter
{
    /**
     * Get the validation rules that apply to the filter request.
     *
     * @return array
     */
    public static function rules(): array
    {
        return [
            'min'   => ['required', 'numeric', 'min:0'],
            'max'   => ['required', 'numeric']
        ];
    }

    /**
     * Apply filter query on related model.
     * @param  \Illuminate\Database\Eloquent\Builder &$query
     */
    public function apply(&$query)
    {
        return $query->where('price', '>=', $this->min)
            ->where('price', '<=', $this->max);
    }
}

Note: You can easily access the values associated with each filter key by using $this->key.

Product model:

use BaraaDark\LaravelFilter\Traits\Filterable;
use App\Http\Filters\Product\ProductPriceRangeFilter;

class Product extends Model
{
    use HasFactory, Filterable;

    protected $guarded = [];

    public function filtersKeys(): array
    {
        return [
            'price-range'   => ProductPriceRangeFilter::class
        ];
    }
}

Applying Filters

Filters can be applied by sending a request with the following structure in the body:

{
    "filters": {
        "filter_key": {
            "filter_class_key": "value",
            "filter_class_key": "value"
        }
    }
}

Example:

{
    "filters": {
        "price-range": {
            "min": 500000,
            "max": 1000000
        }
    }
}

Global vs. Local Scope

If apply_global_scope is set to true in the configuration file, filters will be applied globally to all models when the filters are included in the request. This is not recommended as a general setting since multiple models might be used in the same function, and you might want to apply the filter only to the main model manually.

Configuration

To configure global scope:

// config/laravel-filter.php

return [
    'apply_global_scope' => false, // Set to true to enable global scope
];

Using Local Scope

If apply_global_scope is set to false, you can manually apply the filter in your controller:

use App\Models\SubjectCategory;

public function index(): LengthAwarePaginator
{
    return SubjectCategory::applyFilter()->paginate();
}

Note

Remember to make routes that use filtering either POST or match(['post', 'get']) since the request contains data.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-10