UserResource.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. 'gender_text' => $this->getGenderText(),
  21. 'created_at' => $this->created_at,
  22. 'updated_at' => $this->updated_at,
  23. // 技师相关信息
  24. 'coach' => $this->when($this->coach, function () {
  25. return [
  26. 'id' => $this->coach->id,
  27. 'state' => $this->coach->state,
  28. 'state_text' => $this->coach->state_text,
  29. // 如果有申请记录,返回申请状态
  30. 'application' => $this->when($this->coach->info, function () {
  31. return [
  32. 'id' => $this->coach->info->id,
  33. 'state' => $this->coach->info->state,
  34. 'state_text' => $this->coach->info->state_text,
  35. 'created_at' => $this->coach->info->created_at,
  36. ];
  37. }),
  38. ];
  39. }),
  40. ];
  41. }
  42. /**
  43. * 获取性别文本
  44. */
  45. private function getGenderText(): string
  46. {
  47. return match ($this->gender) {
  48. 0 => '未知',
  49. 1 => '男',
  50. 2 => '女',
  51. default => '未知',
  52. };
  53. }
  54. }