UserResource.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Resources\Client;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class UserResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. return [
  15. 'id' => $this->id,
  16. 'mobile' => $this->mobile,
  17. 'nickname' => $this->nickname,
  18. 'avatar' => $this->avatar,
  19. 'gender' => $this->gender,
  20. 'created_at' => $this->created_at,
  21. 'updated_at' => $this->updated_at,
  22. // 技师相关信息
  23. 'coach' => $this->when($this->coach, function () {
  24. return [
  25. 'id' => $this->coach->id,
  26. 'state' => $this->coach->state,
  27. 'state_text' => $this->coach->state_text,
  28. // 如果有申请记录,返回申请状态
  29. 'application' => $this->when($this->coach->info, function () {
  30. return [
  31. 'id' => $this->coach->info->id,
  32. 'state' => $this->coach->info->state,
  33. 'state_text' => $this->coach->info->state_text,
  34. 'created_at' => $this->coach->info->created_at,
  35. ];
  36. }),
  37. ];
  38. }),
  39. ];
  40. }
  41. }