Download the PHP package abdallahmohammed/laravel-eloquent-filter without Composer
On this page you can find all versions of the php package abdallahmohammed/laravel-eloquent-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-eloquent-filter
Laravel Eloquent Filter
A simple Laravel, and Lumen package to filter models, and relations
Installation
Laravel
via composer
Edit config/app.php (Skip this step if you are using laravel 5.5+)
Copy the package config to your local config
In the config/laravel-eloquent-filter.php
config file. Set the namespace your model filters will reside in
Lumen
This is only required if you want to use the
php artisan make:filter
command.
In bootstrap/app.php
Change The Default Namespace
In bootstrap/app.php
Usage
Generating the filter
You can create a model filter with the following artisan command
Where User
is the Eloquent Model name you are creating the filter for. This will create app/Http/Filters/UserFilter.php
The command also supports psr-4 namespacing for creating filters.
Defining filters
After generating the filter for an Eloquent Model you will find something like that in the filters directory
As you can see there is an protected property called $filters
there you can define all filters for the Model.
For example:
Bear in mind that all methods filter must be based on camel case
Now after defining filters, let's define the methods that will filter the query
Side note: the empty values are not ignored by default so if you want to filter only for non-empty values you have to check if the value is not empty, or you can simply change it for the package config file
Now let's imagine that you want to show a user called John even if the filter is not exists you have to do something like that.
This method will be called if the filter called name
does not exists
Applying The Filter To A Model
Implement the LaravelEloquentFilter\Filterable
trait on any Eloquent Model:
This gives you access to the filter()
method that accepts a BaseFilter instance:
Another example:
Contributing
Any contributions are welcome !!