SuggestTransformer.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\PostDislike;
  13. use App\Models\PostLike;
  14. use App\Traits\UserTrait;
  15. use Carbon\Carbon;
  16. use League\Fractal\TransformerAbstract;
  17. class SuggestTransformer 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. 'show_type' => 0,
  65. 'id' => $post['id'],
  66. 'type' => $post['type'],
  67. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  68. 'uid' => $post['uid'],
  69. 'username' => $post['username'],
  70. 'avatar' => $post['avatar'],
  71. 'topic' => $topic,
  72. 'title' => $post['title'],
  73. 'content' => $post['content'],
  74. 'location' => $post['location'],
  75. 'img' => $post['img'],
  76. 'imgs' => $imgs,
  77. 'video' => $post['video'],
  78. 'pv' => $post->data->pv,
  79. 'praise_count' => $post->data->praise_count,
  80. 'comment_count' => $post->data->comment_count,
  81. 'will_collect_bean' => $post->data->will_collect_bean,
  82. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  83. 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  84. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  85. 'comment' => $comment,
  86. 'is_follow' => $isFollow,
  87. ];
  88. }
  89. }