masterskill / dto
This package is intended for the adoption of the DTO schema in Laravel, which is a system for controlling incoming and outgoing data, in collaboration with the Laravel formRequest, or the Eloquent model
Requires
- php: >=8.0
- laravel/framework: >=8
Requires (Dev)
- spatie/laravel-package-tools: ^1.16
This package is auto-updated.
Last update: 2025-03-03 10:28:46 UTC
README
The project
This package is intended for the adoption of the DTO schema in Laravel, which is a system for controlling incoming and outgoing data, in collaboration with the Laravel formRequest, or the Eloquent model.
Installation
To install the project, you should install it from composer :
composer require masterskill/dto
After that, you should publish the config file.
The DTO class
The Dto class has a different properties to handle. Exemple of the DTO :
<?php namespace App\Http\Dto; class UserDto { public function __construct( public readonly string $name, public readonly string $email ) {} }
Or, eventially, the DTO can implements the IRequestBasedDto or IModelBasedDto interfaces to add some functionnality.
<?php namespace App\Http\Dto; use Masterskill\Dto\Interfaces\IRequestBasedDto; use Masterskill\Dto\Interfaces\IModelBasedDto; use Illuminate\Http\Request; use App\Models\User; class UserDto implements IRequestBased, IModelBasedDto { public function __construct( public readonly string $name, public readonly string $email ) {} public static function fromRequest(Request $request) { return new static("John Doe" , "john.doe@example.com") } public static function fromModel(User $user) { return new static($user->name , $user->email) } }