Coder Social home page Coder Social logo

z-filters's Introduction

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

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.