Download the PHP package holoyan/eloquent-filter without Composer
On this page you can find all versions of the php package holoyan/eloquent-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download holoyan/eloquent-filter
More information about holoyan/eloquent-filter
Files in holoyan/eloquent-filter
Package eloquent-filter
Short Description Eloquent filter for laravel models, easy and powerful
License MIT
Homepage https://github.com/holoyan/eloquent-filter
Informations about the package eloquent-filter
Eloquent Filter
Table of Content
- Introduction
- Requirement
- Installation
- Basic Usage
- Dynamic filters
- Customize column
- Customize value
- Available filters
- Ordering
- Relation Filter
- Nested Filter
- Extending filter
- Custom Rules
- Credits
-
License
Introduction
An easy way to add custom filters to your eloquent models. Powerful, flexible and fully dynamic
Requirement
PHP >= 7.1
Installation
composer require holoyan/eloquent-filter
Basic Usage
Let's say we want to return a list of users filtered by multiple parameters.
For example this is our request url:
/users?email=jo&categories[]=3&categories[]=4&role=admin
$request->all()
will return:
To filter by all those parameters we can simply write
In our User
model we must import Filterable
trait
then create class called UserFilter
in App\Http\Filters
folder
Query returns all users whose name starts with jo
, belongs to category 3 or 4 and have role
admin.
By default, package will look for filter class in App\Http\Filters
folder with name ModelFilter
, but if you want ypu can customize this behavior.
add $filterClass
static property in model.
Dynamic filter
Sometimes you may want to use a dynamic filter depending on conditions. In this case you can pass second argument to filter method which is the filter class:
Customize column
You can also customize your column name
Customize value
Available filters
SimpleRule::make() - by default, this will check for exact match
if you want to use like
comparison type you can use one of those methods:
-
RawRule::make()
- this allows you to specify callback function with your own logic RelationRule::class
- for relation filter, check bellow for more details
Ordering
To order result use OrderRule
rule
Relation filter
Suppose User
has Product
relation, and we want to return all users which have product which name starts with 'cook'
This allows you recursively pass any rules you want
Nested filter
To make nested filter we need to use NestedRule::class
Extending filter
You can create your custom rules with custom logic
Custom Rules
For creating custom rules all you need is to create you class extend from use holoyan\EloquentFilter\Rules\FilterRule
class and implement handle
method
Thats all, now you can use custom rule;
Credits
Inspired by and-m-a
License
MIT