CourseMemberResource.php 1.3 KB

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