thecodemill/eloquent-filter

0.5 2019-09-20 23:24 UTC

This package is auto-updated.

Last update: 2024-04-21 20:10:21 UTC


README

Add simple filterability to Eloquent models.

Installation

Install the EloquentFilter package via Composer:

composer require thecodemill/eloquent-filter

Usage

As the name suggests, this package is intended for use with Eloquent and therefor works seamlessly with Laravel.

EloquentFilter allows you to define filter handlers on any of your app's models to make querying much simpler. A filter handler is very similar to a local scope, but by using the Model::filter() method, any number of scopes may be applied at once without the need for chaining the individual scopes or query modifiers.

Eg.

// With query modifiers
$users = User::where('email', $request->input('email'))
    ->where('age', $request->input('age'))
    ->whereHas('subscription', function ($query) use ($request) {
        $query->where('code', $request->input('subscription'));
    })
    ->get();

// Using EloquentFilter's filter handlers
$users = User::filter($request->all())->get();

This is made possible by adding a filters() method to the model itself. This method should return a key => value array of filter names and their respective closures.

namespace App;

use TheCodeMill\EloquentFilter\Filterable;

class User
{
    use Filterable;
    
    /**
     * Return the filter handlers.
     *
     * @return array
     */
    public static function filters()
    {
        return [
            'email' => function ($query, $value) {
                return $query->where('email', $value);
            },
            'age' => function ($query, $value) {
                return $query->where('age', $value);
            },
            'subscription' => function ($query, $value) {
                return $query->whereHas('subscription', function ($query) use ($value) {
                    $query->where('code', $value);
                });
            }
        ];
    }
}

If using pagination, EloquentFilter allows you to maintain the query parameters in between page requests. To do so, render the pagination links in your Blade template with the addition of the valid filters that are present in the original page request.

// Paginate models in your controller/repository/etc.
$users = User::filter($request->all())->paginate();

// In Blade, append only the valid query parameters:
{{ $users->appends(App\User::validFilters(request()->all()))->links() }}

Author