Download the PHP package kyslik/laravel-filterable without Composer
On this page you can find all versions of the php package kyslik/laravel-filterable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kyslik/laravel-filterable
More information about kyslik/laravel-filterable
Files in kyslik/laravel-filterable
Package laravel-filterable
Short Description Using URL query strings to filter Eloquent queries.
License MIT
Homepage https://github.com/kyslik/laravel-filterable
Informations about the package laravel-filterable
Laravel Filterable
This package allows you to easily handle database filtering through query strings. The idea is taken from one of the Jeffrey's videos (behind the paywall). One quick example might look like this: /users?filter-username=~joe
will result in SQL query select * from users where "username" like '%joe%'
.
Installation
You can install the package via composer:
If you are using Laravel 7:
Laravel will discover the package by itself. If you feel old-school, disable auto-discovery and add Kyslik\LaravelFilterable\FilterableServiceProvider::class
to the providers array in your config/app.php
.
You may continue by publishing configuration by issuing following artisan command php artisan vendor:publish
.
Introduction
Package lets you to create && apply two kinds of filters custom and generic.
Custom filters
Custom filters are just like in Jeffrey's video. You define a logic on a builder instance and package applies it via local scope.
Let's say a product requires displaying recently created records. You create a method recent($minutes = null)
inside a filter class, which returns Builder instance:
Note: full example is shown later on
Generic filters
Generic filters are those defined in config file. By default, the package supports filtering timestamps
, ranges
, ins
, booleans
and strings
.
Default operator matrix for generic filters
operator | accepts | description |
---|---|---|
= |
string |
equal |
!= |
string |
not equal |
> |
string |
greater than |
< |
string |
less than |
>= |
string |
equal or greater than |
<= |
string |
equal or less than |
~ |
string |
like |
!~ |
string |
not like |
>< |
comma separated list | between |
!>< |
comma separated list | not between |
i= |
comma separated list | in |
i!= |
comma separated list | not in |
b= |
1 , 0 , true , false , yes , no |
equal |
b!= |
1 , 0 , true , false , yes , no |
not equal |
t= |
UNIX timestamp | equal |
t!= |
UNIX timestamp | not equal |
t> |
UNIX timestamp | greater than |
t< |
UNIX timestamp | less than |
t>= |
UNIX timestamp | equal or greater than |
t<= |
UNIX timestamp | equal or less than |
t>< |
UNIX timestamp | between |
t!>< |
UNIX timestamp | not between |
Usage
While using both custom or generic filters you must:
- have local scope on model with the signature
scopeFilter(Builder $query, FILTERNAME $filters)
- have particular (
FILTERNAME
) filter class that extends one of:Kyslik\LaravelFilterable\Generic\Filter
class - allows usage of both custom & generic filtersKyslik\LaravelFilterable\Filter
class - allows usage of only custom filters
- call a scope within a controller
make:filter command
You can use the following command to create a new filter.
This will create a new Custom filter in the app/Filters directory. To create a Generic filter just add the --generic
(-g
) flag to the command:
Lastly, you can override the default namespace by changing the namespace config value e.g.
config/filterable.php
Example with custom filters
Let's say you want to use filterable on User
model. You will have to create the filter class App/Filters/UserFilter.php
(php artisan make:filter UserFilter
), specify filterMap()
and filter method (recent(...)
) with the custom logic.
Note:
filterMap()
shall return an associative array where key is a method name and value is either alias or array of aliases
Now add a local scope to the User
model via Filterable:
Finally, call the scope in a controller like so:
Now end-user can visit users?recent
or users?recently
or users?recent=25
and results will be filtered by recent()
method defined in UserFilter
class.
Example with generic filters
Let's say you want to use generic filters on User
model. You will have to create filter class App/Filters/UserFilter.php
(php artisan make:filter UserFilter -g
) and specify $filterables
just like below:
Next, you will have to add a local scope to the User
model via Filterable:
Finally, call the scope in a controller like so:
Now you are ready to filter User
model.
Note: behind the scenes
...\Generic\Filter
class extendsFilter
class, therefore using...\Generic\Filter
also enables you to apply custom filters defined within the filter class
Additional configuration
While using generic filters you may define which generics should be allowed. Define settings()
method in a filter class, see below:
Additional features
Default filtering
In case you need to apply a filter when no filter is applied yet (determined by what query-string contains at the given request), you can use the following code in the controller:
End-user is going be redirected from http://filters.test/users
to http://filters.test/users?recent=2018-10-01 13:52:40&filter-id=!=5
.
In case the filter that you specify as default does not exist Kyslik\LaravelFilterable\Exceptions\InvalidArgumentException
is thrown.
Caution: be careful of infinite redirects
You can read more about the feature in the original issue #10.
JoinSupport for filters
TBA
You can read more about the feature in the original PR #9.
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- kyslik
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-filterable with dependencies
illuminate/support Version 5.6.*|5.7.*|5.8.*
illuminate/database Version 5.6.*|5.7.*|5.8.*
illuminate/http Version 5.6.*|5.7.*|5.8.*