Havingable macro for Laravel Nova fields, allowing filtering of aggregated values.
webard/nova-havingable is a Laravel package for havingable macro for laravel nova fields, allowing filtering of aggregated values..
It currently has 0 GitHub stars and 3.114 downloads on Packagist (latest version v1.1.0).
Install it with composer require webard/nova-havingable.
Discover more Laravel packages by webard
or browse all Laravel packages to compare alternatives.
Last updated
Allow filtering of values calculated by aggregate functions like withSum, withCount, etc.
composer require webard/nova-havingable
Let’s say you have a Resource that calculates aggregated values, such as an Order resource that calculates the total amount of ordered products:
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(),
];
}
}
If you want to make these fields filterable, the filterable() method doesn’t work because it is based on the WHERE clause instead of HAVING.
This package provides a havingable() macro for fields, making them available for filtering.
Simply add the ->havingable() method to your Resource field:
Number::make('Items sum', 'lines_sum_quantity')
->sortable()
->exceptOnForms()
->havingable()
We welcome contributions to improve this plugin! Please follow these steps to contribute:
This project is licensed under the MIT License. See the LICENSE.md file for more details.
For questions or support, please open an issue on GitHub.