Sieve - A simple, clean and elegant way to filter Laravel Eloquent models
Sieve is a Laravel package that allows you to filter retrieved records based on query string values.
Once installed filtration shrinks down from this:
public function index(Request $request)
{
$query = Product::query();
if ($request->has('color')) {
$query->where('color', $request->get('color'));
}
if ($request->has('condition')) {
$query->where('condition', $request->get('condition'));
}
if ($request->has('price')) {
$direction = $request->get('price') === 'highest' ? 'desc' : 'asc';
$query->orderBy('price', $direction);
}
return $query->get();
}
to this:
public function index(Request $request)
{
return Product::filter($request)->get();
}
Installation
This package can be used in Laravel 5.3 or higher. You can install the package via composer:
composer require aldemeery/sieve
The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:
'providers' => [
// ...
Aldemeery\Sieve\FiltersServiceProvider::class,
];