DetailTransformer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/10
  6. * Time: 9:17
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Traits\PostTrait;
  11. use Illuminate\Support\Carbon;
  12. use League\Fractal\TransformerAbstract;
  13. class DetailTransformer extends TransformerAbstract
  14. {
  15. use PostTrait;
  16. public function transform(Post $post)
  17. {
  18. $imgs = [];
  19. foreach($post->imgs as $img){
  20. $imgs[] = $img->img;
  21. }
  22. return [
  23. 'id' => $post['id'],
  24. 'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
  25. 'uid' => $post['uid'],
  26. 'username' => $post['username'],
  27. 'avatar' => $post['avatar'],
  28. 'topic' => $this->getTopic($post['topic_ids']),
  29. 'title' => $post['title'],
  30. 'content' => $post['content'],
  31. 'type' => $post['type'],
  32. 'img' => $post['img'],
  33. 'video' => $post['video'],
  34. 'imgs' => $imgs,
  35. 'location' => $post['location'],
  36. 'pv' => $post->data->pv_real.'/'.$post->data->pv,
  37. 'praise_count' => $post->data->praise_real_count.'/'.$post->data->praise_count,
  38. 'share_count' => $post->data->share_real_count.'/'.$post->data->share_count,
  39. 'comment_count' => $post->data->comment_count,
  40. 'collect_count' => $post->data->collect_real_count.'/'.$post->data->collect_count,
  41. 'create_bean' => $post->data->create_bean,
  42. 'is_suggest' => $post['is_suggest'],
  43. ];
  44. }
  45. }