Releases: WendellAdriel/laravel-validated-dto
v3.2.1
What's Changed
- Fix how to handle enums and carbon objects when transforming DTO by @WendellAdriel in #59
Full Changelog: v3.2.0...v3.2.1
v3.2.0
v3.1.2
What's Changed
- Fix issue when transforming DTOs with nested Collections or Models by @WendellAdriel in #54
Full Changelog: v3.1.1...v3.1.2
v3.1.1
What's Changed
- Fix issue when transforming DTOs with nested DTOs into array or json by @WendellAdriel in #53
Full Changelog: v3.1.0...v3.1.1
v3.1.0
What's Changed
- Added new Attributes and Traits by @WendellAdriel in #51
Full Changelog: v3.0.1...v3.1.0
v3.0.1
What's Changed
- Use a return type of
static
by @simon-tma in #48
New Contributors
- @simon-tma made their first contribution in #48
Full Changelog: v3.0.0...v3.0.1
v3.0.0
What's Changed
- Added logo files by @scottzirkel in #43
- added banner by @ludoguenet in #44
- v3 by @WendellAdriel in #45
New Contributors
- @scottzirkel made their first contribution in #43
- @ludoguenet made their first contribution in #44
Full Changelog: v2.11.1...v3.0.0
v2.11.1
What's Changed
- tests :: refactor some tests by @falahatiali in #39
- Convert Wireable DTO with deeply nested objects to arrays by @bnzo in #41
New Contributors
- @falahatiali made their first contribution in #39
- @bnzo made their first contribution in #41
Full Changelog: v2.11.0...v2.11.1
v2.11.0 - Callable Casting
Changes
- Added the support for defining custom casting with
callables/callbacks
Extended Docs
You can now create new Castable
types for your project by using a callable/callback
:
class CustomDTO extends ValidatedDTO
{
protected function rules(): array
{
return [
'url' => ['required', 'url'],
];
}
protected function defaults(): array
{
return [];
}
protected function casts(): array
{
return [
'url' => function (string $property, mixed $value) {
return new URLWrapper($value);
},
];
}
}
New Contributors
ResourceDTO and Wireable Trait
Changes
- Added a new ResourceDTO class that you can use to wrap, type and transform your API responses
- Added a new Wireable trait that you can use to turn any DTOs into Wireable DTOs to use with Laravel Livewire
Docs for changes
Resource DTOs
If you want to use DTOs to wrap, type and transform your API responses, you can use the ResourceDTO
class.
This class will have the same features as the SimpleDTO
class and will implement the Illuminate\Contracts\Support\Responsable
interface:
class UserResourceDTO extends ResourceDTO
{
public string $name;
public string $email;
public int $age;
// Your DTO methods...
}
Then you can return your DTOs from your controllers:
class UserController extends Controller
{
public function show(int $id)
{
return UserResourceDTO::fromModel(User::findOrFail($id));
}
}
You can also return a collection/list of your DTOs as a response using the ResourceDTO::collection()
method:
class UserController extends Controller
{
public function index()
{
return UserResourceDTO::collection(User::all());
}
}
This way every item in the collection will be converted to a UserResourceDTO
instance before sending
the response to the client, using all the typing, casting and mapping features of your DTO class.
To generate a ResourceDTO
you can use the --resource
flag:
php artisan make:dto UserResourceDTO --resource
Wireable DTOs
If you're using Laravel Livewire, you can turn your DTOs into wireable DTOs
by adding the WendellAdriel\ValidatedDTO\Concerns\Wireable
trait to your DTOs:
class UserDTO extends ValidatedDTO
{
use Wireable;
// Your DTO code...
}