GithubHelp home page GithubHelp logo

jeffersonsimaogoncalves / eloquent-filters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gurgentil/eloquent-filters

1.0 1.0 0.0 33 KB

Add query filters to your Eloquent models with ease.

License: MIT License

PHP 100.00%

eloquent-filters's Introduction

Eloquent Filters

Latest Version GitHub Workflow Status Quality Score MIT Licensed

This package provides a nice and easy way to add query filters to your Eloquent models without turning your model classes into massive God objects.

After setting it up, if you are building an API, for example, you can simply add this piece of code to your controller:

User::filter($request->get('filter'))->get();

Now make a request to /api/users?filter[name]=John and let the fun begin.

Installation

Install the package via composer:

composer require gurgentil/eloquent-filters

Usage

Create a filter builder that extends Gurgentil\EloquentFilters\FilterBuilder. This is where you will register all the filters for your Eloquent model.

namespace App\Filters;

use Gurgentil\EloquentFilters\FilterBuilder;

class UserFilters extends FilterBuilder
{
    protected $availableFilters = [];
}

Add the Gurgentil\EloquentFilters\Filterable trait to your model class. The trait will look for a filter builder for the model inside App\Filters.

Creating filters

Run the artisan command:

php artisan make:filter Users\NameFilter

Register the filter in the builder you created previously:

namespace App\Filters;

use App\Filters\Users\NameFilter;
use Gurgentil\EloquentFilters\FilterBuilder;

class UserFilters extends FilterBuilder
{
    protected $availableFilters = [
        'name' => NameFilter::class,
    ];
}

Last but not least, implement your query in NameFilter:

namespace App\Filters\Users;

use Gurgentil\EloquentFilters\Filter;
use Illuminate\Database\Eloquent\Builder;

class NameFilter implements Filter
{
    /**
     * Apply filter.
     *
     * @return Builder
     */
    public function apply(Builder $builder, $value)
    {
        return $builder->where('name', $value);
    }
}

You can perform filter queries on your model by passing a list of filters to the filter method:

User::filter([
    'name' => 'John',
])->get();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

eloquent-filters's People

Contributors

gurgentil avatar

Stargazers

 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.