PostTransformer.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\UserTrait;
  15. use Carbon\Carbon;
  16. use League\Fractal\TransformerAbstract;
  17. class PostTransformer extends TransformerAbstract
  18. {
  19. use UserTrait;
  20. public function __construct()
  21. {
  22. }
  23. public function transform(Post $post)
  24. {
  25. $imgs = [];
  26. foreach($post->imgs as $img){
  27. $imgs[] = $img['img'];
  28. }
  29. $topic = [];
  30. foreach($post->topic() as $key => $val){
  31. $topic[] = [
  32. 'id' => $key,
  33. 'name' => $val
  34. ];
  35. }
  36. $user = $this->userInfo($post['uid']);
  37. return [
  38. 'id' => $post['id'],
  39. 'type' => $post['type'],
  40. 'created_at' => $post['created_at'],
  41. 'uid' => $post['uid'],
  42. 'username' => $user['username'],
  43. 'avatar' => $user['avatar'],
  44. 'topic' => $topic,
  45. 'title' => $post['title'],
  46. 'content' => $post['content'],
  47. 'location' => $post['location'],
  48. 'img' => $post['img'],
  49. 'imgs' => $imgs,
  50. 'video' => $post['video'],
  51. 'pv' => getNumber($post->data->pv),
  52. 'praise_count' => $post->data->praise_count,
  53. 'collect_count' => $post->data->collect_count,
  54. 'comment_count' => $post->data->comment_count,
  55. 'available_bean' => $post->data->available_bean,
  56. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  57. ];
  58. }
  59. }