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

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.