Skip to content

arvinlp/laravel-search-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-search-filter

Laravel Search Filter page on GitHub

How Use ?

in your controller instead of use routine method like :

$flight = App\Flight::pageination();
//Or
$flight = App\Flight::with('captain')->pageination();
//Or
$flight = App\Flight::where('active', 1)->pageination();
//Or
$flight = App\Flight::with('captain')->where('active', 1)->all();
//Or
$flight = App\Flight::with('captain')->where('active', 1)->get();
//Or
$flight = App\Flight::with('captain')->where('active', 1)->pageination();

With this class you can parsing get url and using one line code for fetching data with Eloquent and filtered data.

$flight = App\SearchFilter::apply( $request, new Flight, 'all', 'captain' );

Function Arguments

SearchFilter::apply( Request, Model, Query Type, Relationships )

Request(Required) : Illuminate\Http\Request $request; Any request laravel can supported like Get, Post, Put, etc. But for using search and filters using Get method !

Model(Required) : Illuminate\Database\Eloquent\Model; model should be extened of Eloquent Model. and just passing to function not more!

Query Type : is String method or can be NULL.

  1. all : Get All Data
  2. get : Get Data
  3. pageination : Get Data With Pageination
When Query Type is Null or not selected by Default is 'pageination'.

Relationships : When you need use Eloquent Relation in your query can send by this arg. this arg like Main scopeWith can parsing array.

    $relation = 'role';
    //or
    $relation = ['role','access'];
  


Add New Custom Filter

in directory "Filters" you can add new filter class.new filter class should be implements Filter and use Eloquent Builder for using functions related. also if you need filter like father_name in your new filter class name is FatherName, under line removed and first character is upper.

for example :

namespace App\SearchFilters\Filters;
use Illuminate\Database\Eloquent\Builder;
class NewFilter implements Filter{
    /**
     * Apply a given search value to the builder instance.
     *
     * @param Builder $builder
     * @param mixed $value
     * @return Builder $builder
     */
    public static function apply(Builder $builder, $value){
        return $builder->where('new_filter', "LIKE", "%".$value."%" );
    }
}

Add Custom Request by PHP And Passed to Class

For Pass Custom Request to Class, you can using merge function in Request Class.
and just pass array with merge function to request :)

    $request->merge([
      "order_by" => "name",
      "order" => "desc"
    ]);
  

Thank you

Arvin Loripour