Giter Site home page Giter Site logo

laravel-eloquent-model-filter's Introduction

Laravel Eloquent model filter

Code Quality Build Status Code Intelligence Status Total Downloads

Now you can filter your model dynamically. This is not a powerful search system, but this can help you with small projects when you need more time in others features and simple filters.

composer require leocarmo/laravel-eloquent-model-filter

Usage

First, implement the Trait on your model, like this:

use LeoCarmo\ModelFilter\Traits\FilterableModel;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{

    use FilterableModel;

}

Now you can use all available methods, we will pass thought later. But after this, some required attributes are required to use all features from this filter.


First attribute is filterable, with these you can set all allowed filters and the operator for this attribute. Like this:

class YourModel extends Model
{

    use FilterableModel;

    protected $filterable = [
        'id',
        'name' => 'LIKE',
        'email',
        'age' => '>=',
        'phone'
    ];

}

The second is filterable_select, with these you set columns to select on query. This is very important for fast queries. If this attribute is not present on model, all columns will be returned (* operator).

class YourModel extends Model
{

    use FilterableModel;

    protected $filterable_select = [
        'id', 'name', 'email', 'age'
    ];

}

Now, all required configurations are set. You can start all your filters like this:

class YourController
{

    public function filter(Request $request)
    {
        return (new YourModel)->filter($request->all())->get();
    }

}

Important: This Trait return Illuminate\Database\Query\Builder instance, so you can use all available methods like paginate() and orderBy() to power up your filter

Tips

The default operator is =, so, for all queries this will be used. You can change this with changeDefaultOperator() method.


When you use LIKE operator, the search will be: %SEARCH%

Available methods

To assume all the control, you can change model default filter configuration in a specific request.

pushFilterableSelect()

This method will push new columns to the select query.


Example: on your model you defined id and name, but in a specific request you want to show the age, you can use this method.

$model->pushFilterableSelect('age');
// OR
$model->pushFilterableSelect(['age', 'created_at']);

pushFilterable()

With this, you can push new columns to allowed filter or change the operator for an existent column.


If you push a column that was defined on model with the attribute filterable and an operator, the original value will be override with the new operator.

// this first example will not overide the original column if the default value has an operator seted, but will push to the allowed filters if was not defined
$model->pushFilterable('age'); 

// this example will overide the original operator
$model->pushFilterable(['age' => '>']);

changeDefaultOperator()

The default operator is = when no operator was defined on model. If you want, you can change this:

$model->changeDefaultOperator('LIKE'); 

Credits

laravel-eloquent-model-filter's People

Contributors

leocarmo avatar

Watchers

 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.