aammui / l9-repository
There is no license information available for the latest version (v0.0.5) of this package.
Repository patterns for Laravel
v0.0.5
2023-08-26 14:48 UTC
Requires
- php: ^8.1
- illuminate/database: ^9.39|^10.0
- league/fractal: ^0.20.1
Requires (Dev)
- orchestra/testbench: ^7.12
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-26 17:40:32 UTC
README
A basic data lever wrapper for Laravel's Eloquent model with the features such as Transformers, Filters, Caching etc.
Installation
composer require aammui/l9-repository
Uses
Transformers
First define a UserTransformer
class UserTransformer extends TransformerAbstract { public function transform(User $user) { return [ 'id' => $user->id, 'name' => $user->name ]; } }
and use it as,
app()->make(UserRepository::class) ->setTransformer(UserTransformer::class) ->get();
Or use with custom includes,
app()->make(UserRepository::class) ->setTransformer(UserTransformer::class, function(\League\Fractal\Manager $manager){ $manager->parseIncludes('role'); }) ->get();
and add role
to availableIncludes
in UserTransformer
protected array $availableIncludes = [ 'role' ]; public function transform(User $user){...} public function includeRole(User $user): Item { $author = $user->role; return $this->item($author, new class extends TransformerAbstract { public function transform(Role $role): array { return [ 'id' => $role->id, 'label' => $role->label, ]; } }); }
Credits
License
The MIT License (MIT). Please see License File for more information.