webard/nova-havingable

为 Laravel Nova 字段提供 Havingable 宏,允许过滤聚合值。

v1.1.0 2024-09-23 18:42 UTC

This package is auto-updated.

Last update: 2024-09-23 18:42:49 UTC


README

允许过滤通过聚合函数(如 withSumwithCount 等)计算出的值。

安装

composer require webard/nova-havingable

描述

假设你有一个计算聚合值的资源,比如一个订单资源,计算已订购产品的总金额

class Order extends Resource
{
    public static $model = \App\Models\Order::class;

    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query
            ->withSum('lines', 'amount')
            ->withSum('lines', 'quantity');
    }

    public function resourceFields(NovaRequest $request): array
    {
        return [
            ID::make('ID', 'id'),

            Number::make('Items Sum', 'lines_sum_quantity')
                ->sortable()
                ->exceptOnForms(),

            Number::make('Amount Sum', 'lines_sum_amount')
                ->sortable()
                ->exceptOnForms(),
        ];
    }
}

如果你想使这些字段可过滤,filterable() 方法不起作用,因为它基于 WHERE 子句而不是 HAVING

此包为字段提供 havingable() 宏,使其可用于过滤。

用法

只需将 ->havingable() 方法添加到你的资源字段

Number::make('Items sum', 'lines_sum_quantity')
    ->sortable()
    ->exceptOnForms()
    ->havingable()

待办事项

  • 添加测试

贡献

我们欢迎贡献来改进这个插件!请按照以下步骤进行贡献

  1. Fork 仓库。
  2. 为你的功能或错误修复创建一个新分支。
  3. 进行更改并使用描述性消息提交更改。
  4. 将您的更改推送到您分叉的仓库。
  5. 向主仓库发起拉取请求。

许可证

本项目采用 MIT 许可证。有关详细信息,请参阅 LICENSE.md 文件。

联系

如有疑问或需要支持,请在 GitHub 上提交问题。