maarsson / eloquent-getter
An Eloquent getter/filter/sorter pattern for Laravel
Package info
github.com/maarsson-io/eloquent-getter
Type:package
pkg:composer/maarsson/eloquent-getter
Requires
- php: ^8.1
- laravel/framework: ^10.0 || ^11.0 || ^12.0 || ^13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpmd/phpmd: ^2.15
- squizlabs/php_codesniffer: ^3.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This package adds an extendable Eloquent model getter/filter/sorter pattern to your Laravel project.
Installation
- Add the package to your Laravel project:
composer require maarsson/eloquent-getter - Publish the config file:
php artisan vendor:publish --tag=eloquent-getter-config
Usage
Models can be easily filtered or sorted using the package. Filter keys in the request without a matching filter method in the getter class are ignored. A sort_by value without a matching sorter method sorts by the table column of the same name, or is ignored if there is no such column.
-
Create a getter class for your existing model:
php artisan make:getter 'YourModel'. -
Add the
Maarsson\EloquentGetter\Traits\GetterableTraittrait to the model:namespace App\Models; use Illuminate\Database\Eloquent\Model; use Maarsson\EloquentGetter\Traits\GetterableTrait; class YourModel extends Model { use GetterableTrait; }
-
Add the required filter method(s) to the created
YourModelGetterclass. Filter method names must be camel cased and must end withFilter:protected function nameFilter(string|null $searchString): \Illuminate\Database\Eloquent\Builder { return $this->builder->where('name', 'LIKE', '%' . $searchString . '%'); }
-
Get the filtered collection using the
filter[]parameter in the query:// HTTP GET //localhost/yourmodel?filter[name]=foo public function index(\App\Getters\YourModelGetter $getter) { return (new YourModel()) ->filter($getter) ->get(); }
-
Add the required sorter method(s) to the created
YourModelGetterclass. Sorter method names must be camel cased and must end withSorter:protected function relatedModelDateSorter(): \Illuminate\Database\Eloquent\Builder { return RelatedModel::select('date') ->whereColumn('this_model_column', 'related_table.column'); }
-
Get the sorted collection using the
sort_byparameter in the query:// HTTP GET //localhost/yourmodel?sort_by=related_model_date public function index(\App\Getters\YourModelGetter $getter) { return (new YourModel()) ->order($getter) ->get(); }
Combined filtering, sorting and paginating
Get the filtered, sorted and paginated result using the helper methods:
// HTTP GET //localhost/yourmodel?filter[name]=foo&page=5&per_page=20&sort_by=related_model_date&sort_order=desc public function index(\App\Getters\YourModelGetter $getter) { return (new YourModel()) ->filter($getter) ->order() ->paginate(); }
The following request parameters are considered:
filter[]default:nullpagedefault:1per_pagedefault:20sort_bydefault:'id'sort_orderdefault:'asc'
Loading attributes and relations
You can even control which model attributes are returned, including relations (and their attributes). Just pass a simple dot-notated array to the withAttributes() method. This example also shows how to combine it with pagination:
// HTTP GET //localhost/yourmodel?filter[name]=foo&page=5&per_page=20&sort_by=related_model_date&sort_order=desc public function index(\App\Getters\YourModelGetter $getter) { return (new YourModel()) ->filter($getter) ->order() ->paginate() ->through( fn ($item) => $item->withAttributes([ 'id', 'name', // a model property 'finalPrice', // even a model accessor 'users', // a relation (with all of its attributes) 'users.posts:id,title', // a relation's relation (with limited attributes) ]) ); }
License
This package is open-sourced software licensed under the MIT license.