Giter Site home page Giter Site logo

eloquent-filter's Introduction

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

eloquent-filter's People

Contributors

nahidulhasan avatar maki10 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.