laravel-learn-bbs/app/Transformers/ReplyTransformer.php

41 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2018-06-10 14:37:37 +00:00
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/10
* Time: 21:52
*/
namespace App\Transformers;
use App\Models\Reply;
use Carbon\Carbon;
use League\Fractal\TransformerAbstract;
class ReplyTransformer extends TransformerAbstract
{
2018-06-10 14:40:18 +00:00
protected $availableIncludes = ['user', 'topic'];
2018-06-10 14:37:37 +00:00
public function transform(Reply $reply)
{
return [
'id' => (int)$reply->id,
'user_id' => (int)$reply->user_id,
'topic_id' => (int)$reply->topic_id,
'content' => $reply->content,
'created_at' => $reply->created_at instanceof Carbon ? $reply->created_at->toDateTimeString() : $reply->created_at,
'updated_at' => $reply->updated_at instanceof Carbon ? $reply->updated_at->toDateTimeString() : $reply->updated_at,
];
}
2018-06-10 14:40:18 +00:00
public function includeTopic(Reply $reply)
{
return $this->item($reply->topic, new TopicTransformer());
}
2018-06-10 14:37:37 +00:00
public function includeUser(Reply $reply)
{
return $this->item($reply->user, new UserTransformer());
}
}