Download the PHP package cerbero/query-filters without Composer
On this page you can find all versions of the php package cerbero/query-filters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cerbero/query-filters
More information about cerbero/query-filters
Files in cerbero/query-filters
Package query-filters
Short Description Filter Laravel Eloquent models based on query parameters.
License MIT
Homepage https://github.com/cerbero90/query-filters
Informations about the package query-filters
Query Filters
Query Filters was fully inspired by this lesson on Laracasts, it provides a simple way to filter Eloquent models based on query parameters of an HTTP request.
Install
Via Composer:
If your version of Laravel is prior to 5.5, you can register this package service provider by adding the following line to the list of providers in config/app.php
:
This package includes a generator for query filter classes that by default are generated in app/QueryFilters
. If you prefer a different path, you can set it in the config/query_filters.php
file after running:
Usage
Imagine to have a route for indexing all actors stored in the database. This route accepts query parameters to filter records, for example:
In this case the route will need to display only actors who won at least one Oscar, are no longer acting but acted in 2000.
This can be achieved by generating a query filters class and optionally defining the allowed query parameters and related variable names via the following Artisan command:
That command will generate and populate with filters the ActorFilters
class:
Please note how filter names are the equivalent camel case form of their related query parameters.
Filters are only applied if their query parameter is present in the HTTP request with a non-empty value, unless they need no value to function (e.g. won_oscar
).
The $query
property lets filters determine how queries change when they are applied:
After filters are defined, Eloquent models can apply them by using the FiltersRecords
trait:
Finally in your route you can filter actors by calling the method filterBy()
of your model and passing the query filters:
Alternatively you can hydrate query filters from a plain array:
Sometimes you may want to silently skip filters if their value is not valid. Instead of setting validation rules in HTTP requests, you may define them in the query filters class.
This avoids a failed validation to stop the filtering process and applies only valid filters while ignoring filters with values that do not pass the validation:
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
Contributing
Please see CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Jeffrey Way
- Laracasts
- Andrea Marco Sartori
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of query-filters with dependencies
illuminate/http Version >=5.4
illuminate/database Version >=5.4
illuminate/console Version >=5.4
illuminate/support Version >=5.4