PostTransformer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/17
  6. * Time: 16:11
  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\PostTrait;
  15. use App\Traits\UserTrait;
  16. use Carbon\Carbon;
  17. use League\Fractal\TransformerAbstract;
  18. class PostTransformer extends TransformerAbstract
  19. {
  20. use UserTrait;
  21. use PostTrait;
  22. public function __construct()
  23. {
  24. }
  25. public function transform(Post $post)
  26. {
  27. $user = $this->userInfo($post['uid']);
  28. $postInfo = $this->getPostInfo($post['id']);
  29. return [
  30. 'id' => $post['id'],
  31. 'type' => $post['type'],
  32. 'created_at' => $post['created_at'],
  33. 'uid' => $post['uid'],
  34. 'username' => $user['username'],
  35. 'avatar' => $user['avatar'],
  36. 'topic' => $this->getTopic($post['topic_ids']),
  37. 'title' => $post['title'],
  38. 'content' => $post['content'],
  39. 'location' => $post['location'],
  40. 'img' => $post['img'],
  41. 'imgs' => $postInfo['imgs'],
  42. 'video' => $post['video'],
  43. 'pv' => getNumber($postInfo['pv']),
  44. 'praise_count' => $postInfo['praise_count'],
  45. 'collect_count' => $postInfo['collect_count'],
  46. 'comment_count' => $postInfo['comment_count'],
  47. 'available_bean' => $postInfo['available_bean'],
  48. 'will_collect_bean' => $postInfo['will_collect_bean'],
  49. ];
  50. }
  51. }