id = $model->id; $dto->username = $model->username; $dto->status2 = $model->status2; $dto->createdAt = $model->created_at ? $model->created_at->toDateTimeString() : ''; $dto->updatedAt = $model->updated_at ? $model->updated_at->toDateTimeString() : ''; // 如果需要包含用户详细信息,并且模型已加载关联 if ($withInfo && $model->relationLoaded('info') && $model->info) { $dto->info = UserInfoDto::fromModel($model->info); } return $dto; } /** * 转换为数组 * * @return array */ public function toArray(): array { $result = [ 'id' => $this->id, 'username' => $this->username, 'status2' => $this->status2->value, 'status2_name' => $this->status2->name, 'created_at' => $this->createdAt, 'updated_at' => $this->updatedAt, ]; if ($this->info !== null) { $result['info'] = $this->info->toArray(); } return $result; } }