SuggestTransformer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Carbon\Carbon;
  15. use League\Fractal\TransformerAbstract;
  16. class SuggestTransformer extends TransformerAbstract
  17. {
  18. public function __construct($uid)
  19. {
  20. $this->uid = $uid;
  21. }
  22. public function transform(Post $post)
  23. {
  24. $imgs = [];
  25. foreach($post->imgs as $img){
  26. $imgs[] = $img['img'];
  27. }
  28. $comment = [];
  29. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
  30. foreach($comments as $item){
  31. $replyCount = $item->reply->count();
  32. $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
  33. $reply = [];
  34. foreach($replies as $val){
  35. $reply[] = [
  36. 'nickname' => $val->nickname,
  37. 'reply_username' => $val->reply_username,
  38. 'content' => $val->content,
  39. ];
  40. }
  41. $comment[] = [
  42. 'id' => $item->id,
  43. 'nickname' => $item->nickname,
  44. 'content' => $item->content,
  45. 'reply_count' => $replyCount,
  46. 'reply' => $reply,
  47. ];
  48. }
  49. return [
  50. 'show_type' => 0,
  51. 'id' => $post['id'],
  52. 'type' => $post['type'],
  53. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  54. 'uid' => $post['uid'],
  55. 'username' => $post['username'],
  56. 'avatar' => $post['avatar'],
  57. 'topic' => $post->topic(),
  58. 'title' => $post['title'],
  59. 'content' => $post['content'],
  60. 'location' => $post['location'],
  61. 'img' => $post['img'],
  62. 'imgs' => $imgs,
  63. 'video' => $post['video'],
  64. 'pv' => $post->data->pv,
  65. 'praise_count' => $post->data->praise_count,
  66. 'comment_count' => $post->data->comment_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_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  70. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  71. 'comment' => $comment,
  72. 'is_follow' => 1,
  73. ];
  74. }
  75. }