An open API service indexing awesome lists of open source software.

https://github.com/bedus-creation/l9-repository


https://github.com/bedus-creation/l9-repository

Last synced: about 1 year ago
JSON representation

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.