Download the PHP package mohamedgaber-intake40/filter-quent without Composer
On this page you can find all versions of the php package mohamedgaber-intake40/filter-quent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mohamedgaber-intake40/filter-quent
More information about mohamedgaber-intake40/filter-quent
Files in mohamedgaber-intake40/filter-quent
Package filter-quent
Short Description An easy way to filter Queries Laravel Eloquent.
License MIT
Homepage https://github.com/mohamedgaber-intake40/filter-quent
Informations about the package filter-quent
Laravel Eloquent Filter
An easy way to filter Queries Laravel Eloquent.
Install (Laravel)
The Package is auto discovered , but you can register Service Provider in config/app.php
Usage
Filter
-
make filter class using this command for example UserFilter
- this will generate UserFilter in app\Filters
-
add the Filter\Traits\HasFilter trait to your model(s):
- this will add filter scope to User model
- note : User model its filter class should be UserFilter by default , but you can override this by overriding getFilterClassName method in the model
-
in App\Filters\UserFilter Class , you have to add methods to filter model properties for example :
- if you want to filter name you have to add filter name method like this
- also, you can filter any relations , example : user has many posts
- now you can filter your model query like this
Notes
- filter method must be filter{property} example to filter name , it will be filterName
- if property is something_something , method will be filterSomethingSomething , example : filterFirstName
- when filter class has many methods like filterName, filterAge these methods will be called only when filter array has property matched with the method name
Sort
-
add the Filter\Traits\HasSortable trait to your model(s):
- add sortable array to your model(s):
this array to define which attributes that are available for sorting
- now you can sort your model query like this
and the request should have query string like this :
- /users?sort[]=id,desc
- /users?sort[]=id,asc
- /users?sort[]=id,asc&sort[]=name,asc (multiple sorting)
Notes
- if query string has additional attribute that doesn't exist in the sortable array , will be ignored
Select
-
add the Filter\Traits\HasSelectable trait to your model(s):
- add selectable array to your model(s):
this array to define which attributes that are available for selecting
- now you can select your model query like this
and the request should have query string like this :
- /users?select=id,name
Notes
- if query string has additional attribute that doesn't exist in the selectable array , will be ignored
- if query string doesn't have select , all attributes will be returned