TopicDetailTransformer.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\InterestCircle;
  10. use App\Models\Topic;
  11. use Carbon\Carbon;
  12. use League\Fractal\TransformerAbstract;
  13. class TopicDetailTransformer extends TransformerAbstract
  14. {
  15. public function __construct($uid)
  16. {
  17. $this->uid = $uid;
  18. }
  19. public function transform(Topic $topic)
  20. {
  21. $isFollow = 0;
  22. if ($this->uid) {
  23. $isFollow = $topic->follow->where('uid', $this->uid)->count() ? 1 : 0;
  24. }
  25. return [
  26. 'id' => $topic['id'],
  27. 'name' => $topic['name'],
  28. 'img' => $topic['img'],
  29. 'follow_count' => getNumber($topic['use_count'] + $topic['base_count']),
  30. 'is_follow' => $isFollow,
  31. 'circle' => $this->getCircleInfo($topic['circle_id'])
  32. ];
  33. }
  34. public function getCircleInfo($circleId)
  35. {
  36. $circleInfo = InterestCircle::find($circleId);
  37. if ($circleInfo) {
  38. $circle['id'] = $circleInfo->id;
  39. $circle['name'] = $circleInfo->name;
  40. } else {
  41. $circle = new \stdClass();
  42. }
  43. return $circle;
  44. }
  45. }