aqjw/filterable

`Filterable` 包是为 Laravel 8.x+ 设计的,提供了创建和管理 Eloquent 模型过滤器的工具。它使得扩展过滤器和将它们应用于过滤记录变得容易。

1.5.0 2023-04-15 21:47 UTC

This package is auto-updated.

Last update: 2024-09-27 22:19:11 UTC


README

Latest Version on Packagist Total Downloads

`Filterable` 包是为 Laravel 8.x+ 设计的,提供了创建和管理 Eloquent 模型过滤器的工具。它使得扩展过滤器和将它们应用于过滤记录变得容易。

开始 🚀

安装

可以使用以下命令通过 Composer 安装此包:

composer require aqjw/filterable

添加 `HasFilters` 特性

安装后,将 `HasFilters` 特性添加到您想要过滤的模型中

use Aqjw\Filterable\HasFilters;

class Product
{
    use HasFilters;

用法

创建过滤器

要创建新的过滤器,请使用 Artisan 的 make:filter 命令

php artisan make:filter ByPrice

您还可以指定要过滤的列

php artisan make:filter ByPrice --column=retail_price

此外,您还可以指定一个过滤器组

php artisan make:filter ByCategory --group=Product

过滤器将创建在以下组文件夹中:`\App\Filters\Product\ByCategory::class`

应用过滤器

创建您的过滤器后,可以将它们应用于模型

Product::filters([
    \App\Filters\ByPrice::class,
    \App\Filters\BySalePrice::class,
])->get();

您可以使用 `or` 操作符应用多个过滤器

Product::filters([
    \App\Filters\ByPrice::class,
    'or',
    \App\Filters\BySalePrice::class,
])->get();

如果您需要分组过滤器,可以将它们放在数组中

Product::filters([
    [
        \App\Filters\ByCategory::class,
        \App\Filters\Product\ByPrice::class,
    ],
    'or',
    [
        \App\Filters\BySubCategory::class,
        \App\Filters\BySalePrice::class,
    ],
])->get();

使用 `key` 方法检查请求参数

过滤器类中的 `key` 方法用于检查请求参数中是否存在某个键。如果键不存在,则过滤器不会被应用。

以下是一个如何使用 `key` 方法的示例

use Aqjw\Filterable\Filter;

class ByPrice extends Filter
{
    public function key()
    {
        // only apply the filter if 'price' is present in the request
        return 'price';
    }

    public function apply($query, $value)
    {
        $query->where('price', $value);
    }
}

在此示例中,只有当请求参数中存在 `price` 键时,`ByPrice` 过滤器才会被应用。

重写 `isActive` 方法

如果您想强制应用过滤器,即使请求参数中不存在相应的键,也可以在过滤器类中重写 `isActive` 方法。

以下是一个如何使用 `isActive` 方法的示例

use Aqjw\Filterable\Filter;

class ByCategory extends Filter
{
    public function key()
    {
        // only apply the filter if 'category' is present in the request
        return 'category';
    }

    public function isActive($request)
    {
        // always force the filter to be applied,
        //  even if 'category' is not present in the request.
        // this will be useful when we want to apply the filter by default,
        //  for example, when displaying all products in a certain category
        return true;
    }

    public function apply($query, $value)
    {
        if ($value) {
            // apply the filter based on the value of 'category' in the request
            $query->where('category', $value);
        } else {
            // if 'category' is not present in the request, default to showing products in the root category (category ID of 1)
            $query->where('category', 1);
        }
    }
}

在此示例中,即使请求参数中不存在 `category` 键,`ByCategory` 过滤器也始终会被应用。如果存在 `category` 键,过滤器将根据其值应用。如果不存在 `category` 键,过滤器将默认显示根类别的产品(类别 ID 为 1)。

许可证

`Filterable` 包是开源软件,受 MIT 许可证的许可。有关更多信息,请参阅许可证文件

贡献

欢迎贡献!如果您想为此项目做出贡献,请按照以下步骤操作

  1. 分支仓库
  2. 创建一个新分支(`git checkout -b feature/your-feature`)
  3. 进行您的更改
  4. 将您的分支推送到您的分支仓库(`git push origin feature/your-feature`)
  5. 打开拉取请求