Blog Detail

06

Jul
Laravel Filterable - Add Filtration Functionality to Models cover image

arrow_back Laravel Filterable - Add Filtration Functionality to Models

Laravel Filterable package adds filtration functionality to Laravel Models. It would be based on Filterable and Query Filter classes. The package will provide commands to generate Filterable and Query Filter classes. By default, it will add some default filtration out of the box to your models like ordering, getting data between two dates, and more.

Requirement

The package requires:

  • PHP 8.0 or higher
  • Laravel 9.x

Installation

You can install the package via composer:

composer require thejano/laravel-filterable

You can publish the config file with:

php artisan vendor:publish --tag="filterable-config"

Usage Example

Imagine you have a url containing the following parameters:

/posts?slug=the-new-web&published=true&category=web-development&tags[]=web&tags[]=laravel&tags[]=flutter

Laravel request all method request()->all() will return something like this:

[
    "slug"        => "the-new-web",
    "published"   => "true",
    "category"    => "web-development",
    "tags"        => [ "web", "laravel", "flutter"],

Normally, you should do the logic one by one to perform the filtration

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        $query = Post::query();

        if ($request->has('title'))
        {
            $query->where('title', 'LIKE', '%' . $request->input('title') . '%');
        }
       
       if ($request->has('published'))
        {
            $query->where('published', (bool) $request->input('published'));
        }

        if ($request->has('category')){
            $query->whereHas('category', function ($query) use ($request)
            {
                return $query->where('category_slug', $request->input('category'));
            });
        }
        
        if ($request->has('tags')){
            $query->whereHas('tag', function ($query) use ($request)
            {
                return $query->where('tag_slug', $request->input('tags'));
            });
        }
        $posts = $query->get();
        return view('posts',compact('posts'));
    }

}

For simple queries, it would be fine, but when you have a bunch of filters, you can not control them and none of them are reusable.

With this package you need to only pass the filterable() scope method to your model before returning the records, check below example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        $posts = Post::filterable()->get();
        return view('posts',compact('posts'));
    }

}

Closing Note

If you have an amazing idea then Codebrisk is always here to convert your ideas into reality. Our team of qualified developers is trained in Laravel custom web app development, CRM software development, and e-commerce app development. We run with a superior level of agility and an immense Laravel ecosystem to customize the software for your business. For more details, please contact us or you can launch a project with us.

Published at : 06-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project