CourseResource.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "teacher"=> UserApi::getById($this->teacher),
  24. "course_count"=>10,
  25. "member_count"=>CourseMember::where('course_id',$this->id)->count(),
  26. "publicity"=> $this->publicity,
  27. "start_at"=> $this->start_at,
  28. "end_at"=> $this->end_at,
  29. "content"=> $this->content,
  30. "content_type"=> $this->content_type,
  31. "cover"=> $this->cover,
  32. "channel_id"=>$this->channel_id,
  33. "created_at"=> $this->created_at,
  34. "updated_at"=> $this->updated_at,
  35. ];
  36. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  37. if($textbook){
  38. $data['anthology_id'] = $textbook->uid;
  39. $data['anthology_title'] = $textbook->title;
  40. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  41. }
  42. if(!empty($this->channel_id)){
  43. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  44. if($channel){
  45. $data['channel_name'] = $channel->name;
  46. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  47. }
  48. }
  49. return $data;
  50. }
  51. }