jaetoole / castable-dto
Cast Eloquent Fields To Standard PHP 8 DTOs (Alpha).
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-05-09 03:25:27 UTC
README
Do you want to harness the power of DTOs with Eloquent? If so, this is the package for you. Using PHP 8 DTOs, you can now cast any array/json eloquent column to a DTO. This package is inspired by this awesome package.
Installation
You can install the package via composer:
composer require jaetoole/castable-dto
It's as simple as that!
Usage
Imagine we have a Data Transfer Object like the below:
<?php class MyDto { public function __construct( public string $myField public int $mySecondField ) { } }
That's good but how would we populate this from an Eloquent array or JSON object?
First off, let's extend the CastableDto
class.
<?php class MyDto extends \JaeToole\CastableDto\CastableDto { public function __construct( public string $myField public int $mySecondField ) { } }
Then, in your model, you can specify the following:
protected $casts => [ 'myArrayField' => MyDto::class ]
Then, when fetching or storing your myArrayField
, it will be automatically converted to your Data Transfer Object.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.