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()
- add tests
We welcome contributions to improve this plugin! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with descriptive messages.
- Push your changes to your forked repository.
- Open a pull request to the main repository.
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.