CourseResource.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Models\Collection;
  7. use App\Models\Channel;
  8. use App\Models\CourseMember;
  9. class CourseResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param \Illuminate\Http\Request $request
  15. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $data = [
  20. "id"=>$this->id,
  21. "title"=> $this->title,
  22. "subtitle"=> $this->subtitle,
  23. "summary"=> $this->summary,
  24. "teacher"=> UserApi::getById($this->teacher),
  25. "course_count"=>10,
  26. "member_count"=>CourseMember::where('course_id',$this->id)->count(),
  27. "publicity"=> $this->publicity,
  28. "start_at"=> $this->start_at,
  29. "end_at"=> $this->end_at,
  30. "content"=> $this->content,
  31. "content_type"=> $this->content_type,
  32. "cover"=> $this->cover,
  33. "channel_id"=>$this->channel_id,
  34. "join"=> $this->join,
  35. "request_exp"=> $this->request_exp,
  36. "created_at"=> $this->created_at,
  37. "updated_at"=> $this->updated_at,
  38. ];
  39. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  40. if($textbook){
  41. $data['anthology_id'] = $textbook->uid;
  42. $data['anthology_title'] = $textbook->title;
  43. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  44. }
  45. if(!empty($this->channel_id)){
  46. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  47. if($channel){
  48. $data['channel_name'] = $channel->name;
  49. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  50. }
  51. }
  52. return $data;
  53. }
  54. }