TopicDetailTransformer.php 805 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/17
  6. * Time: 18:10
  7. */
  8. namespace App\Transformers\Topic;
  9. use App\Models\Topic;
  10. use Carbon\Carbon;
  11. use League\Fractal\TransformerAbstract;
  12. class TopicDetailTransformer extends TransformerAbstract
  13. {
  14. public function __construct($uid)
  15. {
  16. $this->uid = $uid;
  17. }
  18. public function transform(Topic $topic)
  19. {
  20. $isFollow = 0;
  21. if($this->uid){
  22. $isFollow = $topic->follow->where('uid', $this->uid)->count()?1:0;
  23. }
  24. return [
  25. 'id' => $topic['id'],
  26. 'name' => $topic['name'],
  27. 'img' => $topic['img'],
  28. 'follow_count' => getNumber($topic->follow->count() + $topic['base_count']),
  29. 'is_follow' => $isFollow,
  30. ];
  31. }
  32. }