1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/15
- * Time: 11:07
- */
- 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 [
- 'show_type' => 0,
- 'id' => $post['id'],
- 'type' => $post['type'],
- 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
- 'uid' => $post['uid'],
- 'username' => $post['username'],
- 'avatar' => $post['avatar'],
- 'topic' => $post->topic(),
- '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,
- '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,
- ];
- }
- }
|