Skip to content

KayckMatias/z-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zFilters

a simple package to filter your Laravel models without repetitive loops or various if-else

Version Total Downloads License

Installation

You can install the package via composer:

composer require kayckmatias/z-filters

Usage

You just need two things: make a new filter and create a scope in your model to point filter.

To make a new Filter:

php artisan make:zfilter NameFilter

Example:

Example new Filter Image

and configure your filter options according to your preference (read here)

Now, is need make a scope in your model, example:

public function scopeFilterBy(Builder $query, array $filters)
{
    $filter = new UsersFilter($query, $filters);

    return $filter->apply();
}

Configure

zFilters supports three filters type: simple, relation and complex.

Simple Filters:

Simple Filter is a whereIn condition, when you make a simple filter you is saying.

'custom_name' => 'column to verify set whereIn'

Let's assume that your user table has the column "department_id", to make a simple filter you just need to reference how the name of the filter option will be, let's call it "departments" and the column, which would be "department_id"

'departaments' => 'departament_id'

Now the array of values or single value you send in $filters['departments'] will be filtered in whereIn condition on the simple filter

Relation Filters:

Relation Filter is a whereIn condition in a relation, when you make a relation filter you is saying.

'custom_name' => ['relation' => 'column to verify set whereIn']

Let's assume that your User model has the relation with department (departments()), to create a relationship filter where we must get all the users that the department belongs to manager 1 or 2 we can do this:

'departments_manager' => ['departments' => 'manager_id']

Now the array of values or single value you send in $filters['departments_manager'] will be filtered in whereIn condition in departments relation on manager_id column.

Complex Filters

The complex filter can deal with more specific conditions, it accepts a callback function and you can filter however you want, let's go to another example. Assuming your same user table has a name column and a summary column and you expect to do a complex search for both conditions by value, one way to do it would be:

'search' => function ($q, $filterValue) {
    $q->where(function ($q) use ($filterValue) {
        $q->where('name', 'LIKE', "%" . $filterValue . "%");
        $q->orWhere('summary', 'LIKE', "%" . $filterValue . "%");
    });
}

The complex filter option named 'search' will execute the function, the value of $q will be the query builder being formed and the second parameter, $filterValue will be automatically implemented to what is sent in $filters['search'], as it is a callback function you can work on it too, manipulate the values sent, as if it were inside the query builder itself.

License

The MIT License (MIT). Please see License File for more information


Made with ♥ by Kayck Matias

About

A simple package to filter your Laravel Models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages