2017 © Pedro Peláez
 

library presentit-laravel

Transformers for nested Eloquent models and collection for building API with Laravel framework

image

dan-har/presentit-laravel

Transformers for nested Eloquent models and collection for building API with Laravel framework

  • Tuesday, July 4, 2017
  • by danhar
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

presentit-laravel

Presentit adapter for laravel framework., (*1)

Custom presentations and transformation of nested Eloquent models, models relations and collections., (*2)

See full presentit docs here, (*3)

Docs

Installation

Install using composer, (*4)

composer require dan-har/presentit-laravel

Add the presentit service provider to the app config file, (*5)

'providers' => [
    // ...
    Presentit\Laravel\PresentitServiceProvider::class,
]

Transform Eloquent models

Use presentit transformation functionality with any Eloquent model by implementing the Presentable contract and using the PresentsItem trait., (*6)

For example, the User model class with the PresentsItem trait, (*7)

class User extends Authenticatable implements Presentable
{
    use PresentsItem;

    //...
}

To transform the user model use the present method to get a Present instance or use the transfrom method to use a transformer., (*8)

$user = User::find(1);

$user->present()->with(function(User $user){
    return [
        //...
    ];
});

$user->transform(function(User $user){
    return [
        //...
    ];
});

Instead of closure transformer you can pass a transformer class, see presentit docs or example below., (*9)

Transform collections

To transform collections the present and transformWith macros were added to the base collection., (*10)

$posts = Collection::make();

$posts->present()->each(function (Post $post) {
    return [
        //...
    ];
});

$posts->transformWith(function (Post $post) {
    return [
        //...
    ];
});

Model relations that returns collections such as HasMany will also have the presentit transformation functionality, (*11)

The collection presentit api uses the transformWith method because the transform method exists in the base laravel collection., (*12)

class Post implements Presentable
{
    use PresentsItem;

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

$posts = Posts::find(1);

$posts->comments->transformWith(function (Comment $comment) {
    return [
        //...
    ];
});

Transform nested models and relations

To demonstrate the nested model transformation we will use an example of a Post with comments and on each comment users can write comments. So first we use a transformer class for the Post, Comment and User model, (*13)

class UserTransformer
{
    public function transform(User $user) {
        return [
            'name' => ucfirst($user->name),
            'profile_image' => $user->profile_image ?: Hidden::key(),
        ];
    }
}


class CommentTransformer
{
    public function transform(Comment $comment)
    {
        return [
            'text' => $comment->text,
            'datetime' => $comment->created_at->toW3cString(),
            'edited_datetime' => $comment->edited_at ? $comment->edited_at : Hidden::key(),
            'user' => $comment->user->transform(UserTransformer::class),
            'comments' => $comment->comments->transformWith(CommentTransformer::class),
        ];
    }
}


class PostTransformer
{
    public function tranfrom(Post $post)
    {
        return [
            'title' => $post->title,
            'text' => $post->text,
            'user' => $post->user->transform(UserTransformer::class),
            'datetime' => $post->created_at->toW3cString(),
            'comments' => $post->comments->transformWith(CommentTransformer::class),
        ];
    }
}

Then to transform a single post use, (*14)

$post = Post::find(1);

$array = $post->transform(PostTransformer::class)->show();

The Versions

04/07 2017

dev-master

9999999-dev

Transformers for nested Eloquent models and collection for building API with Laravel framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Har

api json rest nested transformer presentit presentit-laravel eloquent-transformer laravel-model-transformer

04/07 2017

1.0.1

1.0.1.0

Transformers for nested Eloquent models and collection for building API with Laravel framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Har

api json rest nested transformer presentit presentit-laravel eloquent-transformer laravel-model-transformer

18/03 2017

1.0.0

1.0.0.0

Transformers for nested Eloquent models and collection for building API with Laravel framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dan Har

api json rest nested transformer presentit presentit-laravel eloquent-transformer laravel-model-transformer