PostTransformer.php 1.6 KB

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