|
@@ -0,0 +1,41 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2019/6/15
|
|
|
+ * Time: 16:40
|
|
|
+ */
|
|
|
+namespace App\Transformers\Post;
|
|
|
+
|
|
|
+use App\Models\PostComment;
|
|
|
+use Carbon\Carbon;
|
|
|
+use League\Fractal\TransformerAbstract;
|
|
|
+
|
|
|
+class CommentTransformer extends TransformerAbstract
|
|
|
+{
|
|
|
+
|
|
|
+ public function transform(PostComment $postComment)
|
|
|
+ {
|
|
|
+ $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
|
|
|
+ $reply = [];
|
|
|
+ foreach($replies as $val){
|
|
|
+ $reply[] = [
|
|
|
+ 'uid' => $val->uid,
|
|
|
+ 'nickname' => $val->nickname,
|
|
|
+ 'avatar' => $val->avatar,
|
|
|
+ 'reply_username' => $val->reply_username,
|
|
|
+ 'content' => $val->content,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ 'id' => $postComment['id'],
|
|
|
+ 'uid' => $postComment['uid'],
|
|
|
+ 'username' => $postComment['username'],
|
|
|
+ 'avatar' => $postComment['avatar'],
|
|
|
+ 'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
|
|
|
+ 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
|
|
|
+ 'reply_count' => $postComment->reply->count(),
|
|
|
+ 'reply' => $reply,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|