SuggestTransformer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 11:07
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostComment;
  12. use App\Models\PostLike;
  13. use Carbon\Carbon;
  14. use League\Fractal\TransformerAbstract;
  15. class SuggestTransformer extends TransformerAbstract
  16. {
  17. public function __construct($uid)
  18. {
  19. $this->uid = $uid;
  20. }
  21. public function transform(Post $post)
  22. {
  23. $imgs = [];
  24. foreach($post->imgs as $img){
  25. $imgs[] = $img['img'];
  26. }
  27. $comment = [];
  28. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
  29. foreach($comments as $item){
  30. $replyCount = $item->reply->count();
  31. $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
  32. $reply = [];
  33. foreach($replies as $val){
  34. $reply[] = [
  35. 'nickname' => $val->nickname,
  36. 'content' => $val->content,
  37. ];
  38. }
  39. $comment[] = [
  40. 'id' => $item->id,
  41. 'nickname' => $item->nickname,
  42. 'content' => $item->content,
  43. 'reply_count' => $replyCount,
  44. 'reply' => $reply,
  45. ];
  46. }
  47. return [
  48. 'show_type' => 0,
  49. 'id' => $post['id'],
  50. 'type' => $post['type'],
  51. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  52. 'uid' => $post['uid'],
  53. 'username' => $post['username'],
  54. 'avatar' => $post['avatar'],
  55. 'topic' => $post->topic(),
  56. 'title' => $post['title'],
  57. 'content' => $post['content'],
  58. 'location' => $post['location'],
  59. 'img' => $post['img'],
  60. 'imgs' => $imgs,
  61. 'video' => $post['video'],
  62. 'pv' => $post->data->pv,
  63. 'praise_count' => $post->data->praise_count,
  64. 'share_count' => $post->data->share_count,
  65. 'comment_count' => $post->data->comment_count,
  66. 'collect_count' => $post->data->collect_count,
  67. 'will_collect_bean' => $post->data->will_collect_bean,
  68. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  69. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  70. 'comment' => $comment,
  71. 'is_follow' => 1,
  72. ];
  73. }
  74. }