PHP code example of dan-har / presentit-laravel
1. Go to this page and download the library: Download dan-har/presentit-laravel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dan-har / presentit-laravel example snippets
'providers' => [
// ...
Presentit\Laravel\PresentitServiceProvider::class,
]
class User extends Authenticatable implements Presentable
{
use PresentsItem;
//...
}
$user = User::find(1);
$user->present()->with(function(User $user){
return [
//...
];
});
$user->transform(function(User $user){
return [
//...
];
});
$posts = Collection::make();
$posts->present()->each(function (Post $post) {
return [
//...
];
});
$posts->transformWith(function (Post $post) {
return [
//...
];
});
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 [
//...
];
});
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),
];
}
}
$post = Post::find(1);
$array = $post->transform(PostTransformer::class)->show();