Starting in v3, you can also use the DTO as it was a Form Request class, so instead of manually creating the DTO instance, you can type-hint the DTO in the controller method, and it will be automatically created for you:
Beware that the fields in the $hidden property of the Model won't be used for the DTO.
From Artisan Commands
You have three ways of creating a DTO instance from an Artisan Command:
From the Command Arguments
<?phpuseApp\DTOs\UserDTO;useIlluminate\Console\Command;classCreateUserCommandextendsCommand{protected $signature ='create:user {name} {email} {password}';protected $description ='Create a new User';/** * Execute the console command. * * @returnint * * @throwsValidationException */publicfunctionhandle() { $dto =UserDTO::fromCommandArguments($this); }}
From the Command Options
<?phpuseApp\DTOs\UserDTO;useIlluminate\Console\Command;classCreateUserCommandextendsCommand{protected $signature ='create:user { --name= : The user name } { --email= : The user email } { --password= : The user password }';protected $description ='Create a new User';/** * Execute the console command. * * @returnint * * @throwsValidationException */publicfunctionhandle() { $dto =UserDTO::fromCommandOptions($this); }}
From the Command Arguments and Options
<?phpuseApp\DTOs\UserDTO;useIlluminate\Console\Command;classCreateUserCommandextendsCommand{protected $signature ='create:user {name} { --email= : The user email } { --password= : The user password }';protected $description ='Create a new User';/** * Execute the console command. * * @returnint * * @throwsValidationException */publicfunctionhandle() { $dto =UserDTO::fromCommand($this); }}