duuany/eloquent-filters

A simplest Eloquent query filter package.

2.2 2022-05-26 23:08 UTC

This package is auto-updated.

Last update: 2024-10-27 04:13:03 UTC


README

A simplest Eloquent Model URL query filter package for your Laravel

Instalation

You can install via composer:

Laravel 5.5+

$ composer require duuany/eloquent-filters

###Laravel 5.4 or above

$ composer require duuany/eloquent-filters:1.1

Add the service provider to your config/app.php

Duuany\EloquentFilters\EloquentFiltersServiceProvider::class

Optionally, you can publish config file to override package configuration

$ php artisan vendor:publish --provider="Duuany\EloquentFilters\EloquentFiltersServiceProvider" --tag="config"

Usage

In your model, add the following HasFilters trait

class User extends Model 
{
    use HasFilters;
}

Create a UserFilter class anywhere in your app folder. Filter classes defines array of applicable filters. For each filter added to array of filters, you need to implement the filter logic.

You can create filters via artisan command

$ php artisan make:filter FilterName
class UserFilter extends EloquentFilters
{
    protected $filters = [
        'order', 'popular', ....
    ]; 
    
    protected function order($column)
    {
        return $this->builder->orderBy($column, 'desc');
    }
}

You can pass multiple parameters to your filters, like this:

    protected function order($column, $sort = 'desc')
    {
        return $this->builder->orderBy($column, $sort);
    }

When passing more than one paramenter to filter, make sure use a delimiter in your query string.

http://myurl.dev?order=id:asc

The default delimiter its :, but your can modify overriding the protected $delimiter property.

Example:
class UserFilter extends EloquentFilters
{
    protected $delimiter = '|';
    
    ...
}

In query string...

http://myurl.dev?order=id|asc

... keep in mind, to not use special query strings characters like delimiters.

Magic Calls

Optionally, you can ommit ->builder:

// this...
return $this->builder->orderBy($column, $sort);

// can be called like this...
return $this->orderBy($column, $sort);

Now in your application you can use the filters as the following:

class UserController extends Controller
{
    public function index(UserFilter $filters)
    {
        $users = User::filter($filters)->get();
    }
}

When the user access the URL http://localhost/?order=id, the order filter will be applied on the User model.

Testing

$ ./vendor/bin/phpunit

Credits

License

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