12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace App\Http\Resources\Client;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- class UserResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'mobile' => $this->mobile,
- 'nickname' => $this->nickname,
- 'avatar' => $this->avatar,
- 'gender' => $this->gender,
- 'gender_text' => $this->getGenderText(),
- 'created_at' => $this->created_at,
- 'updated_at' => $this->updated_at,
- // 技师相关信息
- 'coach' => $this->when($this->coach, function () {
- return [
- 'id' => $this->coach->id,
- 'state' => $this->coach->state,
- 'state_text' => $this->coach->state_text,
- // 如果有申请记录,返回申请状态
- 'application' => $this->when($this->coach->info, function () {
- return [
- 'id' => $this->coach->info->id,
- 'state' => $this->coach->info->state,
- 'state_text' => $this->coach->info->state_text,
- 'created_at' => $this->coach->info->created_at,
- ];
- }),
- ];
- }),
- ];
- }
- /**
- * 获取性别文本
- */
- private function getGenderText(): string
- {
- return match ($this->gender) {
- 0 => '未知',
- 1 => '男',
- 2 => '女',
- default => '未知',
- };
- }
- }
|