2018-06-10 10:04:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: xing
|
|
|
|
* Date: 2018/6/10
|
|
|
|
* Time: 17:46
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Transformers;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\Topic;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
|
|
|
|
class TopicTransformer extends TransformerAbstract
|
|
|
|
{
|
2018-06-10 11:28:01 +00:00
|
|
|
|
|
|
|
protected $availableIncludes = ['user', 'category'];
|
|
|
|
|
2018-06-10 10:04:03 +00:00
|
|
|
public function transform(Topic $topic)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $topic->id,
|
|
|
|
'title' => $topic->title,
|
|
|
|
'body' => $topic->body,
|
|
|
|
'user_id' => (int)$topic->user_id,
|
|
|
|
'category_id' => (int)$topic->category_id,
|
|
|
|
'reply_count' => (int)$topic->reply_count,
|
|
|
|
'view_count' => (int)$topic->view_count,
|
|
|
|
'last_reply_user_id' => (int)$topic->last_reply_user_id,
|
|
|
|
'excerpt' => $topic->excerpt,
|
|
|
|
'slug' => $topic->slug,
|
|
|
|
'created_at' => $topic->created_at instanceof Carbon ? $topic->created_at->toDateTimeString() : $topic->created_at,
|
|
|
|
'updated_at' => $topic->updated_at instanceof Carbon ? $topic->updated_at->toDateTimeString() : $topic->updated_at,
|
|
|
|
];
|
|
|
|
}
|
2018-06-10 11:28:01 +00:00
|
|
|
|
|
|
|
public function includeCategory(Topic $topic)
|
|
|
|
{
|
|
|
|
return $this->item($topic->category, new CategoryTransformer());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeUser(Topic $topic)
|
|
|
|
{
|
|
|
|
return $this->item($topic->user, new UserTransformer());
|
|
|
|
}
|
2018-06-10 10:04:03 +00:00
|
|
|
}
|