PHP Classes
elePHPant
Icontem

PHP Eloquent Find Filter: Compose filters to find object matching conditions

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-09-25 (18 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 48 This week: 20All time: 9,619 This week: 16Up
Version License PHP version Categories
eloquent-filter 1.0The PHP License5PHP 5, Databases, Design Patterns
Description Author

This package can compose filters to find object matching conditions.

It provides base classes and a trait that can be used to define filter classes that can be used to define conditions to retrieve data objects using the Eloquent package.

Applications can create query filter classes based on this package base class and trait to define the parameters of the queries that will apply to the filters.

Innovation Award
PHP Programming Innovation award nominee
September 2019
Nominee
Vote
Eloquent is a popular package to be used with the Laravel framework develop PHP applications that need to store and retrieve objects in databases using the Object-Relational mapping approach. Eloquent can be used for that purpose.

This package can be used to create filters to setup conditions that determine which objects are retrieved from a database using the Eloquent package.

Since it provides traits, it is easy to create Eloquent filters with this package using existing classes that use the traits to extend the functionality of those classes.

Manuel Lemos
  Performance   Level  
Name: Nahidul Hasan <contact>
Classes: 3 packages by
Country: Bangladesh Bangladesh
Age: ???
All time rank: 354547 in Bangladesh Bangladesh
Week rank: 19 Up2 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 2x

 

Details

Laravel Eloquent Filter

Latest Stable Version Total Downloads Latest Unstable Version License

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

Installation

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.

<?php  
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.

<?php  
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 :

<?php  
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: https://github.com/nahidulhasan/laravel-eloquent-query-filtering

Thanks to :

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

License

Eloquent-Filter for Laravel is open-sourced software licensed under the MIT license

  Files folder image Files  
File Role Description
Files folder imagesrc (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  src  
File Role Description
  Plain text file EloquentFilterServiceProvider.php Class Class source
  Plain text file Filterable.php Class Class source
  Plain text file QueryFilters.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:48
This week:20
All time:9,619
This week:16Up