A small package that will allow you to easily and configurably filter and sort your query
- Customize sorting and filtering for each parameter
- Easy to set default options
- Structures the code
- Easily expandable
Install dsoloview/laravel-filters-sort from composer
composer require dsoloview/laravel-filters-sort
For the model, you need to add traits HasFilters and HasSort
Create filter class for your model and extends it from dsoloview\LaravelFiltersSort\Filters\QueryFilter
Сreate a method for each parameter you will receive from the filter request
For example:
applicaitons?filters[categories][]=7&filters[search]=myApp
Create sort class for your model and extends it from dsoloview\LaravelFiltersSort\Sort\QuerySort
Сreate a method for each parameter you will receive from the sort request
For example:
applicaitons?sort=id&direction=asc
You can set default param for sort
protected const SORT_DEFAULT = 'param'
And for direction
protected const DIRECTION_DEFAULT = 'asc|desc'
Traits add scopes filter(QueryFilter)
and sort(QuerySort)
to your model
You can pass your filter and sort classes to scopes like in the screenshot.
After it you can make get()
or paginate()
like in example.
For support, telegram @dsoloview.