|
@@ -8,31 +8,39 @@
|
|
namespace App\Transformers\Post;
|
|
namespace App\Transformers\Post;
|
|
|
|
|
|
use App\Models\PostComment;
|
|
use App\Models\PostComment;
|
|
|
|
+use App\Traits\UserTrait;
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
use League\Fractal\TransformerAbstract;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
|
|
class CommentTransformer extends TransformerAbstract
|
|
class CommentTransformer extends TransformerAbstract
|
|
{
|
|
{
|
|
-
|
|
|
|
|
|
+ use UserTrait;
|
|
public function transform(PostComment $postComment)
|
|
public function transform(PostComment $postComment)
|
|
{
|
|
{
|
|
$replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
|
|
$replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
|
|
$reply = [];
|
|
$reply = [];
|
|
foreach($replies as $val){
|
|
foreach($replies as $val){
|
|
|
|
+ $userComment = $this->userInfo($val->uid);
|
|
|
|
+ $replyUsername = '';
|
|
|
|
+ if($val->reply_uid){
|
|
|
|
+ $userReply = $this->userInfo($val->reply_uid);
|
|
|
|
+ $replyUsername = $userReply['username'];
|
|
|
|
+ }
|
|
$reply[] = [
|
|
$reply[] = [
|
|
'uid' => $val->uid,
|
|
'uid' => $val->uid,
|
|
- 'username' => $val->username,
|
|
|
|
- 'avatar' => $val->avatar,
|
|
|
|
- 'reply_username' => $val->reply_username,
|
|
|
|
|
|
+ 'username' => $userComment['username'],
|
|
|
|
+ 'avatar' => $userComment['avatar'],
|
|
|
|
+ 'reply_username' => $replyUsername,
|
|
'content' => $val->is_delete?'该评论已被删除':$val->content,
|
|
'content' => $val->is_delete?'该评论已被删除':$val->content,
|
|
'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
|
|
'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
|
|
];
|
|
];
|
|
}
|
|
}
|
|
|
|
+ $user = $this->userInfo($postComment['uid']);
|
|
return [
|
|
return [
|
|
'id' => $postComment['id'],
|
|
'id' => $postComment['id'],
|
|
'uid' => $postComment['uid'],
|
|
'uid' => $postComment['uid'],
|
|
- 'username' => $postComment['username'],
|
|
|
|
- 'avatar' => $postComment['avatar'],
|
|
|
|
|
|
+ 'username' => $user['username'],
|
|
|
|
+ 'avatar' => $user['avatar'],
|
|
'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
|
|
'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
|
|
'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
|
|
'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
|
|
'reply_count' => $postComment->reply->count(),
|
|
'reply_count' => $postComment->reply->count(),
|