Data Transfer Objects with validation for Laravel applications
Data Transfer Objects with validation for Laravel applications
You can install the package via composer:
composer require wendelladriel/laravel-validated-dto
You can publish the config file with:
php artisan vendor:publish --tag="validated-dto"
Create a DTO by extending ValidatedDTO, declaring typed properties, and defining the validation rules for incoming data:
…
Create an instance from an array, request, JSON string, Eloquent model, or artisan command:
$dto = UserDTO::fromArray([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 's3CreT!@1a2B',
]);
$dto->name; // John Doe
$dto->toArray();
You can also inject a DTO into a controller action and let the service container resolve it from the current request:
use Illuminate\Http\JsonResponse;
final class StoreUserController
{
public function __invoke(UserDTO $dto): JsonResponse
{
return response()->json($dto->toArray());
}
}
Access the full documentation here.
Please see the changelog for more information on what has changed recently.
Thank you for considering contributing to Validated DTO for Laravel! You can read the contribution guide here.
Please review our security policy on how to report security vulnerabilities.
Validated DTO for Laravel is open-sourced software licensed under the MIT license.
No open issues yet, or sync has not completed.