Mohamed Benhida

admin@devma.net

January 19, 2018

Filters on Laravel

We all use filters on our laravel projects .

Today we will see the right way to make filters on Laravel.

We have posts and each post has the owner. so we will try to filter the posts with the owner id and order the id DESC

First of all we create a PostFilters on App\Filters Folder

App\Filters\PostFilters.php

<?php

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class PostFilters {

    protected $filters = ['by'];
    protected $query;

    public function apply(Builder $query)
    {
        $this->query = $query;

        $this->by(1);

        return $this->query;
    }

    public function by($value)
    {
        $this->query->where('user_id', $value);

        return $this;
    }
}

So far we have the by filter that will give us the posts belongs to this user.

We add scopeFilter on Model Post.php

App\Post.php

use Illuminate\Database\Eloquent\Builder;
use App\Filters\PostFilters;

public function scopeFilter(Builder $query,PostFilters $filter)
    {
        return $filter->apply($query);
    }

routes/web.php

Route::get('posts',function(App\Filters\PostFilters $filters) {
    return App\Post::Filter($filters)->get();
});

If we open the posts url it will give all the posts belongs to user_id => 1 because in the PostFilters we forced the id value to 1

filters

now we will make the request url tell us wich user we need to filter their posts ?by=1

App\Filters\PostFilters.php

<?php

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class PostFilters {

    protected $filters = ['by'];
    protected $query;
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function apply(Builder $query)
    {
        $this->query = $query;

        foreach ($this->request->only($this->filters) as $key => $value)
        {
            $this->$key($value);
        }

        return $this->query;
    }

    public function by($value)
    {
        $this->query->where('user_id', $value);

        return $this;
    }
}

If we add ?by=5 to our link it will give us the posts belongs to this user

filter

For example now i want to order the post id to desc

so we just need to add function OrderID or something like that it is your choice

?orderID=true it will order the ID DESC

App\Filters\PostFilters.php

<?php

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class PostFilters {

    protected $filters = ['by','orderID'];
    protected $query;
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function apply(Builder $query)
    {
        $this->query = $query;

        foreach ($this->request->only($this->filters) as $key => $value)
        {
            $this->$key($value);
        }

        return $this->query;
    }

    public function by($value)
    {
        $this->query->where('user_id', $value);

        return $this;
    }

    public function orderID($value)
    {
        if($value == "true")
        {
            $this->query->orderBy('ID','desc');
        }
        return $this;
    }

}

I hope you enjoyed reading this post. If we have question don't hesitate to ask on comments section.

© Mohamed Benhida.

Blog | Packages | About