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,
]
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)
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 [
//...
];
});
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();