|
@@ -9,31 +9,67 @@
|
|
|
namespace App\Transformers\Post;
|
|
|
|
|
|
use App\Models\Post;
|
|
|
+use App\Models\PostCollect;
|
|
|
+use App\Models\PostComment;
|
|
|
+use App\Models\PostLike;
|
|
|
use Carbon\Carbon;
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
|
|
class SuggestTransformer extends TransformerAbstract
|
|
|
{
|
|
|
+ public function __construct($uid)
|
|
|
+ {
|
|
|
+ $this->uid = $uid;
|
|
|
+ }
|
|
|
public function transform(Post $post)
|
|
|
{
|
|
|
+ $imgs = [];
|
|
|
+ foreach($post->imgs as $img){
|
|
|
+ $imgs[] = $img['img'];
|
|
|
+ }
|
|
|
+ $comment = [];
|
|
|
+ $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
|
|
|
+ foreach($comments as $item){
|
|
|
+ $replyCount = $item->reply->count();
|
|
|
+ $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
|
|
|
+ $reply = [];
|
|
|
+ foreach($replies as $val){
|
|
|
+ $reply[] = [
|
|
|
+ 'nickname' => $val->nickname,
|
|
|
+ 'content' => $val->content,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $comment[] = [
|
|
|
+ 'id' => $item->id,
|
|
|
+ 'nickname' => $item->nickname,
|
|
|
+ 'content' => $item->content,
|
|
|
+ 'reply_count' => $replyCount,
|
|
|
+ 'reply' => $reply,
|
|
|
+ ];
|
|
|
+ }
|
|
|
return [
|
|
|
- 'type' => 0,
|
|
|
+ 'show_type' => 0,
|
|
|
'id' => $post['id'],
|
|
|
- 'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
|
|
|
+ 'type' => $post['type'],
|
|
|
+ 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
|
|
|
'uid' => $post['uid'],
|
|
|
'username' => $post['username'],
|
|
|
'avatar' => $post['avatar'],
|
|
|
'topic' => $post->topic(),
|
|
|
- 'content' => subtext($post['content'], 20),
|
|
|
+ 'title' => $post['title'],
|
|
|
+ 'content' => $post['content'],
|
|
|
'location' => $post['location'],
|
|
|
+ 'img' => $post['img'],
|
|
|
+ 'imgs' => $imgs,
|
|
|
+ 'video' => $post['video'],
|
|
|
'pv' => $post->data->pv,
|
|
|
'praise_count' => $post->data->praise_count,
|
|
|
- 'share_count' => $post->data->share_count,
|
|
|
'comment_count' => $post->data->comment_count,
|
|
|
- 'collect_count' => $post->data->collect_count,
|
|
|
- 'create_bean' => $post->data->create_bean,
|
|
|
- 'is_suggest' => $post['is_suggest'],
|
|
|
- 'is_hide' => $post['is_hide'],
|
|
|
+ 'will_collect_bean' => $post->data->will_collect_bean,
|
|
|
+ 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
|
|
|
+ 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
|
|
|
+ 'comment' => $comment,
|
|
|
+ 'is_follow' => 1,
|
|
|
];
|
|
|
}
|
|
|
}
|