Laravel Eloquent Filter Tutorial for Beginners

Laravel Eloquent Filter Tutorial for Beginners

Laravel Eloquent Filter is a simple package that helps us filter Eloquent data using query filters.

This simple package helps you filter Eloquent data using query filters.     


Run the following command:

composer require nahidulhasan/eloquent-filter 

Getting started

Use the trait NahidulHasan\EloquentFilter\Filterable in your eloquent model.

Create a new class by extending the class NahidulHasan\EloquentFilter\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.

Let's assume you want to allow to filter articles data. Please see the following code.

namespace App\Models;  

use Illuminate\Database\Eloquent\Model;
use NahidulHasan\EloquentFilter\Filterable;

class Article extends Model
{ use Filterable;


  • The attributes that are mass assignable.
  • @var array
  • / protected $fillable = [ 'title', 'body' ]; }

Create ArticleFilter class extending QueryFilters.

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use NahidulHasan\EloquentFilter\QueryFilters;

class ArticleFilters extends QueryFilters


  • Filter by Title.
  • @param $title
  • @return Builder
  • @internal param $name
  • @internal param string $level
  • / public function title($title) { return $this->builder->where('title', 'like', '%' .$title.'%'); }

With this class we can use the http query string : title=article_name or any combination of these filters. It is up to you to define if you will use AND wheres or OR.

Now in the controller you can apply these filters like as described in below :

namespace App\Http\Controllers;

use App\Filters\ArticleFilters;
use App\Models\Article;
use Illuminate\Http\Request;


  • Class ArticleController
  • @package App\Http\Controllers
  • / class ArticleController extends Controller
    { /**
    • Display a listing of the resource.
    • @param ArticleFilters $filters
    • @return \Illuminate\Http\Response
    • @internal param Request $request
    • / public function index(ArticleFilters $filters) {
      $articles = Article::filter($filters)->paginate(5);
      return view('articles.index',compact('articles')) ->with('i', (request()->input('page', 1) - 1) * 5); } }

If you go to this link you will get all code:

Thanks to :

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

What’s New in Laravel 6.0

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

Laravel 6 Authentication Tutorial

laravel php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[]( ""), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer]( "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[]( "")** has always been an industry leader for companies and business owners looking to hire...