TopicPostTransformer.php 3.0 KB

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