This simple package helps you filter Eloquent data using query filters.
Run the following command:
composer require nahidulhasan/eloquent-filter
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.
<?php namespace App\Models;use Illuminate\Database\Eloquent\Model;
use NahidulHasan\EloquentFilter\Filterable;class Article extends Model
{
use Filterable;/**
Create ArticleFilter class extending QueryFilters.
<?php
namespace App\Filters;use Illuminate\Database\Eloquent\Builder;
use NahidulHasan\EloquentFilter\QueryFilters;class ArticleFilters extends QueryFilters
{/**
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 :
<?php
namespace App\Http\Controllers;use App\Filters\ArticleFilters;
use App\Models\Article;
use Illuminate\Http\Request;/**
If you go to this link you will get all code: https://github.com/nahidulhasan/laravel-eloquent-query-filtering
Thanks to : https://github.com/laracasts/Dedicated-Query-String-Filtering
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Follow me on Facebook | Twitter
☞ Laravel 6 CRUD Application Tutorial
☞ Laravel 6 Image Upload Tutorial
☞ Laravel 6 Authentication Tutorial
#laravel #php #web-development