CourseMemberResource.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\ChannelApi;
  6. use App\Models\Course;
  7. class CourseMemberResource extends JsonResource
  8. {
  9. /**
  10. * Transform the resource into an array.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  14. */
  15. public function toArray($request)
  16. {
  17. $data = [
  18. "id"=>$this->id,
  19. "user_id"=> $this->user_id,
  20. "course_id"=> $this->course_id,
  21. "role"=> $this->role ,
  22. "user"=> UserApi::getByUuid($this->user_id),
  23. "editor"=> UserApi::getByUuid($this->editor_uid),
  24. "status"=> $this->status,
  25. 'channel_id'=> $this->channel_id,
  26. "created_at"=> $this->created_at,
  27. "updated_at"=> $this->updated_at,
  28. ];
  29. if($this->channel_id){
  30. $channel = ChannelApi::getById($this->channel_id);
  31. if($channel){
  32. $data['channel'] = $channel;
  33. }
  34. }
  35. if(!empty($request->get('request_course'))){
  36. $course = Course::find($this->course_id);
  37. $data['course'] = $course;
  38. }
  39. return $data;
  40. }
  41. }