SimpleDTO API
Complete API reference for SimpleDTO.
Creation Methods
Section titled “Creation Methods”fromArray(array $data): static
Section titled “fromArray(array $data): static”Create from array.
$dto = UserDTO::fromArray($data);fromJson(string $json): static
Section titled “fromJson(string $json): static”Create from JSON.
$dto = UserDTO::fromJson($json);fromModel(Model $model): static
Section titled “fromModel(Model $model): static”Create from Eloquent model.
$dto = UserDTO::fromModel($user);fromRequest(Request $request): static
Section titled “fromRequest(Request $request): static”Create from HTTP request.
$dto = UserDTO::fromRequest($request);validateAndCreate(array $data): static
Section titled “validateAndCreate(array $data): static”Validate and create.
$dto = UserDTO::validateAndCreate($_POST);Validation Methods
Section titled “Validation Methods”validate(): void
Section titled “validate(): void”Validate DTO.
$dto->validate();isValid(): bool
Section titled “isValid(): bool”Check if valid.
if ($dto->isValid()) { // ...}getErrors(): array
Section titled “getErrors(): array”Get validation errors.
$errors = $dto->getErrors();Serialization Methods
Section titled “Serialization Methods”toArray(): array
Section titled “toArray(): array”Convert to array.
$array = $dto->toArray();toJson(): string
Section titled “toJson(): string”Convert to JSON.
$json = $dto->toJson();toXml(): string
Section titled “toXml(): string”Convert to XML.
$xml = $dto->toXml();toModel(string|Model $model): Model
Section titled “toModel(string|Model $model): Model”Convert to Eloquent model.
$user = $dto->toModel(User::class);Conditional Methods
Section titled “Conditional Methods”with(string $context): static
Section titled “with(string $context): static”Set context.
$dto = $dto->with('admin');include(array $properties): static
Section titled “include(array $properties): static”Include lazy properties.
$dto = $dto->include(['posts', 'comments']);only(array $properties): static
Section titled “only(array $properties): static”Include only specified properties.
$dto = $dto->only(['name', 'email']);except(array $properties): static
Section titled “except(array $properties): static”Exclude specified properties.
$dto = $dto->except(['password', 'token']);See Also
Section titled “See Also”- SimpleDTO Guide - Complete guide
- Creating DTOs - Creation methods
- Validation - Validation guide
- Serialization - Serialization guide