morningtrain/laravel-filters

Query filters for Laravel

2.11.3 2021-09-13 13:37 UTC

README

This package provides a series of filters that can be used to constrain a Laravel DB Query based on request values.

Installation

Via Composer

$ composer require morningtrain/laravel-filters

Included Filters

All filters can be used using the base Filter class at: MorningTrain\Laravel\Filters\Filter. It should not be confused with MorningTrain\Laravel\Filters\Filters\Filter that are the base class all filters are extending.

Basic

Filter by a basic variable static variable.

The following example look for filter_request_parameter in the request, and apply it to the query for the column of the same name.

Filter::by('filter_request_parameter')

It corresponds to:

$q->where(
    'filter_request_parameter', 
    '=',
    request()->get('filter_request_parameter')
)

Like in the following example, it is possible to add a closure as the second parameter.

The closure will get the query as well as the filter value as parameters. It makes it possible to apply some custom logic.

Filter::by('filter_request_parameter', function($query, $value) {
    /// Custom logic for applying $value to $query
})

Always

Always run a closure and apply something to the query.

Filter::always(function($query) {

})

With

Eager load relations on the query.

Filter::with(['company', 'roles'])

Paginate

Apply pagination to the query. It is important that this filter is applied last.

It expects the request values $page and $per_pageto be present.

Filter::paginate()

Order

The order filter can be configured to sort the data by specific columns or using scopes.

To specify the columns to sort by, call the only method on the filter. The following example will look for sort[id] and sort[created_at] in the request.

The values of sort[<column_name>] can be either asc or desc.

Filter::order()->only(['id', 'created_at'])

Calling the scope method, it is possible to also configure any scopes to be used. It will be looking in the request for any sort keys that match the scope name.

Filter::order()
    ->only(['id', 'created_at'])
    ->scopes(['orderByName'])

Default values can also easily be added:

Filter::order()
    ->only(['id', 'created_at'])
    ->scopes(['orderByName'])
    ->defaultValue(['orderByName' => 'desc'])

Credits

This package is developed and actively maintained by Morningtrain.

 _- _ -__ - -- _ _ - --- __ ----- _ --_  
(         Morningtrain, Denmark         )
 `---__- --__ _ --- _ -- ___ - - _ --_ ´ 
     o                                   
    .  ____                              
  _||__|  |  ______   ______   ______ 
 (        | |      | |      | |      |
 /-()---() ~ ()--() ~ ()--() ~ ()--() 
--------------------------------------