morningtrain / laravel-filters
Query filters for Laravel
Installs: 2 824
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^7.2|^8.0
- illuminate/database: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/http: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- morningtrain/laravel-support: ^1.0
- dev-master
- 2.x-dev
- 2.14.0
- 2.13.0
- 2.12.0
- 2.11.3
- 2.11.2
- 2.11.1
- 2.11.0
- 2.10.1
- 2.10.0
- 2.9.0
- 2.8.1
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0
This package is auto-updated.
Last update: 2024-10-08 08:16: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_page
to 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
. ____
_||__| | ______ ______ ______
( | | | | | | |
/-()---() ~ ()--() ~ ()--() ~ ()--()
--------------------------------------