Download the PHP package baraadark/laravel-filter without Composer
On this page you can find all versions of the php package baraadark/laravel-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-filter
LaravelFilter
LaravelFilter is a package designed to simplify the process of filtering table fields in a Laravel project. It provides a straightforward way to implement custom query filters for your models.
Getting started
Installation
Please check the official laravel installation guide for server requirements before you start. Official Documentation
You can install the package via composer:
Next, publish the configuration file:
Usage
Applying the Filterable Trait
Use the Filterable trait in your models to enable filtering.
Override filterKeys function
You should override the filtersKeys method to return an array of filter keys and their corresponding filter classes.
Example:
The filtersKeys method returns an associative array where the keys are the names of the filter keys expected from the request, and the values are the filter classes that contain the query logic.
Creating a Filter Class
To create a Filter class, run the command:
You will be prompted to enter the class name and the related model name. The generated file will be located at App\Http\Filters\ModelName.
Filter Class Structure
Example Filter Class:
Note: You can easily access the values associated with each filter key by using $this->key.
Product model:
Applying Filters
Filters can be applied by sending a request with the following structure in the body:
Example:
Global vs. Local Scope
If apply_global_scope is set to true in the configuration file, filters will be applied globally to all models when the filters are included in the request. This is not recommended as a general setting since multiple models might be used in the same function, and you might want to apply the filter only to the main model manually.
Configuration
To configure global scope:
Using Local Scope
If apply_global_scope is set to false, you can manually apply the filter in your controller:
Note
Remember to make routes that use filtering either POST or match(['post', 'get']) since the request contains data.