The abelaguiar/filter-laravel
package provides easy to use functions to simples filters in laravel.
composer require abelaguiar/filter-laravel
Use php artisan
to view the list of commands associated with filters.
php artisan filter:model <Model>
php artisan filter:field <Name>
When creating a filter, inside the class we have an array, where you will put the fields you want to use as filters in your model.
Example filter class with fields implemention:
use App\Filters\Fields\TitleField;
use AbelAguiar\Filter\AbstractFilter;
class PostClass extends AbstractFilter
{
protected $filters = [
'title' => TitleField::class
];
}
Example fields class:
class TitleField
{
public function filter($builder, $value)
{
return $builder->where('title', $value);
}
}
Created the filters, in the model that you want to connect filters, you will put:
use AbelAguiar\Filter\RequestFilter;
class Post
{
use RequestFilter;
...
If you want to set up a different filter path, use the variable below within the model:
protected static $filter = 'App\Filters\PostFilter';
...
Finally, when calling your model anywhere in your application, just call the method filter
by passing the request, thus returning all data from the buca, as in the example below:
Post::filter($request)->get()