Download the PHP package maarsson/eloquent-getter without Composer
On this page you can find all versions of the php package maarsson/eloquent-getter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maarsson/eloquent-getter
More information about maarsson/eloquent-getter
Files in maarsson/eloquent-getter
Package eloquent-getter
Short Description An Eloquent getter/filter/sorter pattern for Laravel
License MIT
Informations about the package eloquent-getter
Eloqent getter package
This package adds and extendable Eloquent model getter/filter/sorter pattern to your Laravel project.
Installation
- Add package to your Laravel project:
composer require maarsson/eloquent-getter
- Publish config file
php artisan vendor:publish --tag=eloquent-getter-config
Usage
Models can be easily filtered or sorted using the package. Filter keys and sorter methods in the request without matching function in the getter class will be ignored.
-
Create getter class to your existing model:
php artisan make:getter 'YourModel'
. -
Add the
Maarsson\EloquentGetter\Traits\GetterableTrait
trait to the model: -
Add the required filtering method(s) to the created
YourModelGetter
class. Filter method names must be camel cased and must end withFilter
: -
Get the filtered collection using the
filter[]
parameter in the query -
Add the required sorting method(s) to the created
YourModelGetter
class. Sorter method names must be camel cased and must end withSorter
: - Get the sorted collection using the
sort_by
parameter in the query
Combined filtering, sorting and paginating
Get the filtered, sorted and paginated result by the helper methods.
The following request parameters are considered:
filter[]
default:null
page
default:1
per_page
default:20
sort_by
default:'id'
sort_order
default:'asc'
Loading attributes and relations
You can even control the model attributes to be fetched, including the relations (and its attributes). Just use simple dot-notated array passed to the withAttributes()
method. In this example you can also see how to combine this with the pagination.
License
This package is open-sourced software licensed under the MIT license.