masoudi / laravel-sieve
A Laravel package to create better query filters
0.3
2022-07-16 12:40 UTC
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
README
The Sieve is a Laravel framework package to create query filters much more easy and clean.
How to use
Install
install package with Composer
composer require masoudi/laravel-sieve
Create A filter
Create a new filter by running the following artisan command
php artisan make:filter IndexPostsFilter
Filter will be create at app/Http/Filters path.
use Masoudi\Sieve\Filter; class IndexPostsFilter extends Filter { /** * Apply conditions on model * * @param \Illuminate\Http\Request $request * @param \Illuminate\Database\Query\Builder $builder * * @return \Illuminate\Database\Query\Builder */ function filter(Request $request, Builder $builder) { $builder = $builder->where("is_active", true) ->where('published', true) ->whereHas('comments', function($query){ $query->where('slug', '=', $request->slug); }); return $builder; } }
Add Filterable trait to model
Use Filterable
trait at model;
use Masoudi\Sieve\Filterable; class Post extends Model { use Filterable; }
Add filters to model
Post::filter(new IndexPostsFilter)->get();