userId = $model->user_id; $dto->status = $model->status; $dto->google2faSecret = $model->google2fa_secret; $dto->nickname = $model->nickname; $dto->avatar = $model->avatar; $dto->wxId = $model->wx_id; $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : ''; $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : ''; // 如果需要包含用户手机信息,并且模型已加载关联 if ($withPhone && $model->relationLoaded('user_phone') && $model->user_phone) { $dto->phone = UserPhoneDto::fromModel($model->user_phone); } return $dto; } /** * 转换为数组 * * @return array */ public function toArray(): array { $result = [ 'user_id' => $this->userId, 'status' => $this->status->value, 'status_name' => $this->status->name, 'google2fa_secret' => $this->google2faSecret, 'nickname' => $this->nickname, 'avatar' => $this->avatar, 'wx_id' => $this->wxId, 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt, ]; if ($this->phone !== null) { $result['phone'] = $this->phone->toArray(); } return $result; } }