The Argonaut DTO for Laravel is a lightweight Data Transfer Object (DTO) package that supports nested casting, recursive serialization, and validation out of the box. Ideal for service layers, APIs, and architecture workflows:
class UserDTO extends ArgonautDTO { public string $username; public string $email; protected array $casts = [ 'username' => 'string', 'email' => 'string', ]; public function rules(): array { return [ 'username' => ['required', 'string'], 'email' => ['required', 'email'], ]; } }
Using the package's assember classes, you can map raw inputs like objects or arrays into DTOs:
class UserDTOAssembler extends ArgonautAssembler { public static function toUserDTO(object $input): UserDTO { return new UserDTO([ 'username' => $input->display_name, 'email' => $input->email, ]); } } // Using the assembler $user = UserDTOAssembler::assemble([ 'display_name' => 'jdoe', 'email' => 'jdoe@example.com', ], UserDTO::class); // Using the `fromArray method: $user = UserDTOAssembler::fromArray($userData, UserDTO::class);
Once you've assembled the DTO, you can validate the object:
$user->validate(); // Throws ValidationException $user->validate(throw: false); // Returns array of errors $user->isValid(); // true/false
Main Features
- Deep nested transformation and casting
- Type-safe data conversion
- Validation using Laravel’s validator
- Casts transform values into other DTOs, Collections, dates, arrays, etc.
- Explicit attribute prioritization
- Clean serialization (toArray, toJson)
- Consistent data shape enforcement across boundaries
You can learn more about this package, get full installation instructions, and view the source code on GitHub.