Download the PHP package novius/laravel-dto without Composer
On this page you can find all versions of the php package novius/laravel-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download novius/laravel-dto
More information about novius/laravel-dto
Files in novius/laravel-dto
Package laravel-dto
Short Description A simple and extensible DTO package for Laravel
License AGPL-3.0-or-later
Homepage https://github.com/novius/laravel-dto
Informations about the package laravel-dto
Laravel DTO
A simple and extensible DTO (Data Transfer Objects) package for Laravel.
This package allows you to define structured data objects with built-in validation, default values, and automatic casting, while supporting both camelCase and snake_case naming conventions.
Installation
You can install the package via composer:
Artisan Command
You can easily generate a new DTO class using the following Artisan command:
By default, the class will be created in the app/Dtos directory.
Basic Usage
To create a DTO, simply extend the Novius\LaravelDto\Dto class and define your properties.
If you try to pass a property that is not defined in the class, an InvalidArgumentException will be thrown.
Features
Validation
Override the rules() method to define Laravel validation rules for your properties.
You can also customize validation messages and attributes by overriding the messages() and attributes() methods:
A ValidationException is thrown if the data does not comply with these rules during instantiation or modification via a setter.
Default Values
Override the defaults() method to define default values.
Casting
The package supports the same cast types as Laravel's Eloquent models. It also automatically infers the cast type from the native PHP type of your properties.
If you need more control, you can still override the casts() method. Explicit casts always take precedence over native type inference.
Supported types: int, float, string, bool, array, object, date, datetime, immutable_date, immutable_datetime, decimal:x, json, encrypted.
You can also cast properties to Backed Enums or Fluent objects:
You can also specify a custom format for date and datetime casts, which will be used when calling toArray():
Magic Getters, Setters and Fluent Interface
You can access your properties via magic methods. Setters automatically apply validation and casting.
The package also supports a fluent interface (methods with the same name as the property):
CamelCase / Snake_case Support
If your properties are defined in snake_case in your class, you can access them in camelCase seamlessly.
Array Conversion
The toArray() method returns all properties (public, protected, or private). It recursively handles nested DTOs.
Dates are formatted according to their cast:
Y-m-dfordateandimmutable_dateY-m-d H:i:sfordatetimeandimmutable_datetime(MySQL format)
Property Mapping
You can map property names to different keys when converting the DTO to an array using the #[Map] attribute or the map() method.
The map() method takes precedence over the attribute.
Configuration via Attributes
In addition to methods, you can use PHP attributes to configure your properties directly.
Validation Rules
Use the #[Rules] attribute to define validation rules.
Default Values
Use the #[DefaultValue] attribute to define default values.
Custom Casting
Use the #[Cast] attribute to define a custom cast type.
Note: Methods (rules(), defaults(), casts()) always take precedence over attributes.
Excluding Properties from DTO Mechanisms
You can use the #[ExcludeFromDTO] attribute to completely exclude a property from all DTO mechanisms (constructor instantiation, magic getters/setters, fluent interface, validation, and toArray() output).
This is useful for properties that you want to keep in the class for internal use only, without them being part of the Data Transfer Object's public data flow.
Excluded properties are not present in the array representation:
Testing
The package uses Pest for testing.
Static Analysis
License
This project is licensed under the AGPL-3.0-or-later License.
All versions of laravel-dto with dependencies
illuminate/contracts Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
illuminate/validation Version ^11.0|^12.0|^13.0