Download the PHP package davidoc26/eloquent-filter without Composer
On this page you can find all versions of the php package davidoc26/eloquent-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download davidoc26/eloquent-filter
More information about davidoc26/eloquent-filter
Files in davidoc26/eloquent-filter
Package eloquent-filter
Short Description Simple filter system for building queries
License MIT
Informations about the package eloquent-filter
Eloquent Filters
Simple filter system for building queries
Requirements
- PHP 8.0+
- Laravel 8, 9, 10
Installation
Introduction
Filters allow you to apply restrictions/rules to create a query. It's like middlewares.
There are two types of filters:
1) Filter 2) RequestFilter (gives you access to the Request instance)
Filters can also have their own arguments (using HasArguments trait)
Usage
Using Filterable
To start using filters, you need to use Filterable trait on your model.
To define filters, override the getFilters() method in your model and return the filters. If no filters have been defined, no filtering will be performed.
Creating new filter
To create a basic filter use the command:
This command will create a filter inside app/Filters directory.
Creating new RequestFilter
If you need a filter that has a Request instance available, create a RequestFilter:
Filter arguments
If you want your filter to have arguments (for example default values) use the HasArguments trait on your filter.
To set the arguments, specify them in your model's getFilters() method:
Then, you can get your arguments in your filter using dynamic properties
To see all the arguments you have defined in the model, use the getArguments() filter method
Filter Packs
Filter packs allows you to collect several filters in one pack. The main purpose of a filter pack is to apply the same filters to models.
To create a filter pack use command:
It will create filter pack in app/Filters/Packs directory:
Then you can apply filter pack to your model:
Applying Filters
To apply the filters specified in the getFilters() method, use filter() on your model.
To use only the filters you need, use the withFilters() method and pass the required filters into it, this method will ignore the filters that were specified in your model.
Post::withFilters([
LoadRelationshipFilter::class,
OffsetFilter::class
])->get();
All versions of eloquent-filter with dependencies
illuminate/support Version ^8.0 || ^9.0 || ^10.0
illuminate/container Version ^8.0 || ^9.0 || ^10.0
illuminate/database Version ^8.0 || ^9.0 || ^10.0