Download the PHP package bssphp/laraveldto without Composer
On this page you can find all versions of the php package bssphp/laraveldto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bssphp/laraveldto
More information about bssphp/laraveldto
Files in bssphp/laraveldto
Package laraveldto
Short Description A strongly typed Data Transfer Object integration for Laravel
License MIT
Informations about the package laraveldto
Laravel DTO
A strongly typed Data Transfer Object for Laravel without magic for PHP 8.0+
This package extends the functionality of bssphp/DTO to provide more narrow usecases for Laravel applications.
Laravel-DTO serves as an intermediate and reusable layer between request input & validation and model attribute population.
Contents
- Installation
- Usage
- Validation
- Populate models
- Populate DTO from request
- Combined usage
- Type casting
- Best practices
- Testing
Installation
Usage
All data objects must extend the bssphp\laraveldto\AbstractModelData
class.
Validation
When attaching the available validation rules and even built-in rules instances.
This will throw a Illuminate\Validation\ValidationException
if any rule does not pass.
Populate Models
You can attach a model to any DTO using the #[ForModel(Model::class)]
attribute.
To associate DTO properties with Model attributes, you need to attach the #[ModelAttribute()]
attribute to each property.
If no parameter is passed to the #[ModelAttribute]
attribute, DTO uses the property name itself.
Create DTO and store to model
Attributes saved in Person
model
Note: You can also pass an existing model to the toModel()
method.
Note: When passing no existing model to the toModel()
method, default values declared in the DTO will be populated. If a model is passed as argument toModel($model)
default values will not override existing model attributes.
Populate DTO from request input data
When attaching the #[RequestAttribute]
attribute, DTO uses the property name itself.
The controller
Request input data
The PersonData
DTO instance
Combined usage
Of course all those attributes start to make sense if used together. You can attach all attributes separately of make use of the #[ValidationRule]
attributes.
Both properties in the following example behave exactly the same. Use as you prefer.
Request input data
The controller
Nested data
In some cases you also want to create realted models with a single HTTP call. In this case you can make use of the #[NestedModelData(NestedData::class)]
which will populate the DTO property with n instances of the defined DTO.
Note that we will not attach an #[ModelAttribute]
attribute to the $address
DTO property since it should not be set to a model attribute.
All attributes attached to the nested DTO will just work as expected.
Request input data
The controller
Type Casting
Type casts will convert any given value to a specified type.
Built-in type casts
CastToDate
The #[CastToDate]
attribute will respect your customly defined date class from Date::use(...)
.
You can also specify a custom date class to be used by passing the date class name as single argument #[CastToDate(MyDateClass::class)]
.
Custom type casts
You can declare custom type cast attributes by simply implementing the CastInterface
interface and attaching an attribute.
Best practices
Make sure to add a @method
PHPDoc comment like shown below to allow IDE and static analyzer support when calling the toModel()
method.
TODO
- [x] Allow array validation rules
field.*
& Map into nested DTO - [x] Add correct validation exception error messages from nested fields
- [x] Pass existing model to
toModel()
method - [x] Create DTO from existing model
- [ ] Only run validation rules if data provided from request