https://github.com/webard/nova-havingable
Havingable macro for Laravel Nova fields, allowing filtering of aggregated values.
https://github.com/webard/nova-havingable
Last synced: 3 months ago
JSON representation
Havingable macro for Laravel Nova fields, allowing filtering of aggregated values.
- Host: GitHub
- URL: https://github.com/webard/nova-havingable
- Owner: webard
- License: mit
- Created: 2024-08-22T10:23:35.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-23T18:42:46.000Z (over 1 year ago)
- Last Synced: 2025-01-29T09:16:34.789Z (over 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Nova Havingable
Allow filtering of values calculated by aggregate functions like `withSum`, `withCount`, etc.
## Installation
```sh
composer require webard/nova-havingable
```
## Description
Let’s say you have a Resource that calculates aggregated values, such as an Order resource that calculates the total amount of ordered products:
```php
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.
## Usage
Simply add the `->havingable()` method to your Resource field:
```php
Number::make('Items sum', 'lines_sum_quantity')
->sortable()
->exceptOnForms()
->havingable()
```
## TODO
- [ ] add tests
## Contributing
We welcome contributions to improve this plugin! Please follow these steps to contribute:
1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Make your changes and commit them with descriptive messages.
4. Push your changes to your forked repository.
5. Open a pull request to the main repository.
## License
This project is licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for more details.
## Contact
For questions or support, please open an issue on GitHub.