Skip to content

This package make it easy to filter any resource.

Notifications You must be signed in to change notification settings

larasquad/filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Larasquad Filter

This package allows you to filter resource based on a request in a simple way and makes you controller code clean.

Installation

You can install the package via composer:

composer require larasquad/filter

Basic usage

Make a filter class and extend it by

use Larasquad\Filter\Filter

or, you can generate it by

php artisan make:filter User

It will generate a filter class on you app\Filters directory.

<?php
namespace  App\Filters;

use Larasquad\Filter\Filter;

class  UserFilter  extends  Filter
{

    /**
     *  Whitelisted request filterable attributes
     *
     * @return array
     */
    protected $filterable = [];

}

Specify the $request attributes which you want to filter in $filterable array.

    protected $filterable = ["first_name", "last_name", "email"];

Note that, if you put an attribute on the $filberable array which is not available in database column, in these case, you have to write a method on it.

Example,

    protected $filterable = ["first_name", "last_name", "email", "from", "to" ];

In these case, from and to attribute are not exists in my database column but I have send the attributes via $request, then I have to specify the from and to method else it will get an error.

    public function from($value)
    {
        $this->query->whereDate('date', '>', $value);
    }

    public function to($value)
    {
        $this->query->whereDate('to', '>', $value);
    }

Now, inject the class in you controller method where you have injected the Request $request class

public  function  index(Request $request)
{
	//
}

Replace the Request $request from your method with UserFilter $filter

public  function  index(UserFilter $filter)
{
	//
}

It will automaticall initiate the Request $request in it's parent constructor. Now use the Filterable trait in you model.

use Larasquad\Filter\Traits\Filterable;

class  User  extends  Authenticatable
{
	use Filterable;
}

It's done. Now you can you the filter method in your controller and pass the $filter object in the method.

public  function  index(UserFilter $filter)
{
	$users = User::filter($filter)->get();
	return  view('pages.users.index', compact('users'));
}

By default, It filters the model by using Laravel default where clause. Here, $request input name is used as column and $request input value is used as search value You can override the search query by writing a method on your Filter Class . You have to name it like the model database column name or the camelCase or snake_case of it.

/**
* Filters the first_name column of the resource
*
* @return  void
*/
public  function  firstName($value)
{
	$this->query->where('first_name', 'like', "%{$value}%");
}

License

The MIT License (MIT)

About

This package make it easy to filter any resource.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages