Download the PHP package bakhadyrovf/easy-filter without Composer
On this page you can find all versions of the php package bakhadyrovf/easy-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bakhadyrovf/easy-filter
More information about bakhadyrovf/easy-filter
Files in bakhadyrovf/easy-filter
Package easy-filter
Short Description This package allows you to filter queries with your own methods.
License MIT
Informations about the package easy-filter
Easy Query Filter
This is a package that filter queries with user's custom methods.
Dependencies
- PHP >= 8.0
- Laravel >= 9.0
Installation
Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
Copy the package config to your local config with the publish command.
Usage
Important:
The namespace for filters by default App\Filters and each filter class before creating expects that you already have Eloquent Model with the namespace standards as follows: |
Filter | Model |
---|---|---|
App\Filters\UserFilter |
App\Models\User |
|
App\Filters\Dashboard\ArticleFilter |
App\Models\Dashboard\Article |
|
App\EloquentFilters\TagFilter |
App\Models\Tag |
First of all you must create filter class:
This command creates ArticleFilter class in your project's app/Filters folder.
Also this command adds trait Filterable to your Eloquent Model.
You can change base folder's name in your config file (Namespace will be changed as well):
Also, you can generate a filter class with subfolder:
Filter class will be located in app/Filters/Dashboard folder.
The newly created class will looks like this:
If the namespace of your Model or Filter does not match the above, you can use --model
option:
For example - we have model with namespace App\Models\Article
, we want to create a filter class with namespace App\Filters\Dashboard\ArticleFilter
:
In order for a filter that does not match the namespace with the model, you must add method on your App\Models\Article
class:
This method will point to a specific filter class.
Now, you can write your methods inside filter class. Let's add first method and try to filter our query. In app/Filters/ArticleFilter:
Method Arguments
- $builder - Illuminate\Database\Eloquent\Builder
- $value - Value taken from request
And you can try to filter query using filter()
method on your Eloquent Model.
This method is basic Eloquent Scope so you can use it as usually.
In app\Http\Controllers\ArticleController:
All parameters that are responsible for filtering must be in query:
If your parameter is in snake case, you don't need to create a method with the same case, because it doesn't match php standards. The package itself converts the parameter to camel case.
For example if your parameter is first_name:
Method will be looks like this:
Multiple values
If your parameter can take multiple values, you can use brackets:
As usual, these values will be in the method's second argument
Ignoring parameters
For example if you want to ignore post_ids parameter from filtering:
You can provide exceptions array of method names or query parameters as an argument to filter()
method:
Multiple filters
If you have multiple filter classes for one model, you can provide specific filter class as a second argument.
This filter class will be used even if you have a provideFilter()
method on the model: