https://github.com/bedus-creation/l9-repository
https://github.com/bedus-creation/l9-repository
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/bedus-creation/l9-repository
- Owner: bedus-creation
- Created: 2021-11-29T07:04:47.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-26T14:49:00.000Z (almost 3 years ago)
- Last Synced: 2025-02-13T22:26:05.029Z (over 1 year ago)
- Language: PHP
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Laravel L9-Repository
A basic data lever wrapper for Laravel's Eloquent model with the features such as Transformers, Filters, Caching etc.
### Installation
```bash
composer require aammui/l9-repository
```
### Uses
* [Transformers](https://github.com/bedus-creation/l9-repository#transformers)
### Transformers
First define a `UserTransformer`
```php
class UserTransformer extends TransformerAbstract
{
public function transform(User $user)
{
return [
'id' => $user->id,
'name' => $user->name
];
}
}
```
and use it as,
```php
app()->make(UserRepository::class)
->setTransformer(UserTransformer::class)
->get();
```
Or use with custom includes,
```php
app()->make(UserRepository::class)
->setTransformer(UserTransformer::class, function(\League\Fractal\Manager $manager){
$manager->parseIncludes('role');
})
->get();
```
and add `role` to `availableIncludes` in `UserTransformer`
```php
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
- [Bedram Tamang](https://github.com/bedus-creation/)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.