DetailTransformer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 Illuminate\Support\Carbon;
  11. use League\Fractal\TransformerAbstract;
  12. class DetailTransformer extends TransformerAbstract
  13. {
  14. public function transform(Post $post)
  15. {
  16. $imgs = [];
  17. foreach($post->imgs as $img){
  18. $imgs[] = $img->img;
  19. }
  20. return [
  21. 'id' => $post['id'],
  22. 'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
  23. 'uid' => $post['uid'],
  24. 'username' => $post['username'],
  25. 'avatar' => $post['avatar'],
  26. 'topic' => $post->topic(),
  27. 'title' => $post['title'],
  28. 'content' => $post['content'],
  29. 'type' => $post['type'],
  30. 'img' => $post['img'],
  31. 'video' => $post['video'],
  32. 'imgs' => $imgs,
  33. 'location' => $post['location'],
  34. 'pv' => $post->data->pv_real.'/'.$post->data->pv,
  35. 'praise_count' => $post->data->praise_real_count.'/'.$post->data->praise_count,
  36. 'share_count' => $post->data->share_real_count.'/'.$post->data->share_count,
  37. 'comment_count' => $post->data->comment_count,
  38. 'collect_count' => $post->data->collect_real_count.'/'.$post->data->collect_count,
  39. 'create_bean' => $post->data->create_bean,
  40. 'is_suggest' => $post['is_suggest'],
  41. ];
  42. }
  43. }