Skip to content

rezamasoudi/sieve

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Sieve

The Sieve is a Laravel framework package to create query filters much more easy and clean.

Latest Version on Packagist Total Downloads

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();