Skip to content

enimiste/laravel-eloquent-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel 5.5 eloquent models filters

Install

Require this package with composer using the following command:

composer require "enimiste/laravel-eloquent-filters:5.5.*"

Usage :

Use the trait Com\NickelIT\Filterables\Filterable in your eloquent model.
Create a new class by extending the class Com\NickelIT\Filterables\QueryFilters and define your custom filters as methods with one argument. Where function names are the filter argument name and the arguments are the value.
Example :

use Illuminate\Database\Eloquent\Builder;

class LessonQueryFilters extends QueryFilters
{
    /**
     * @param string $order
     * @return Builder
     */
    public function popular($order = 'desc'): Builder
    {
        return $this->builder()->orderBy('views', $order);
    }

    /**
     * @param $level
     * @return Builder
     */
    public function difficulty($level): Builder
    {
        return $this->builder()->where('difficulty', $level);
    }

    /**
     * @param string $order
     * @return Builder
     */
    public function length($order = 'asc'): Builder
    {
        return $this->builder()->orderBy('length', $order);
    }
}

With this class we can use the http query string : popular=desc&difficulty=beginner&length or any combination of these filters. It is up to you to define if you will use AND wheres or OR.
In the controller you can apply these filters like :

class LessonController extends Controller
{
    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $bag = new HttpRequestFiltersBag($request);
        //$bag = new ArrayFiltersBag(['popular' => 'asc', 'difficulty' => 'beginner']);
        $filter = new LessonQueryFilters($bag);
        return Lesson::filter($filter)->get();//you can send results to a blade view
    }
}

Thanks to :

https://github.com/laracasts/Dedicated-Query-String-Filtering

License

The Laravel Eloquent Filters is open-sourced software licensed under the MIT license

About

Apply filters on laravel eloquent models easy

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages