This package adds artisan:make
commands to generate an Action or Data Transfer Object.
- Install Spatie Generators
composer require ge-tracker/spatie-generators
- The service provider will be automatically loaded - installation complete!
Running the following command will generate a TestAction
into the app/Actions
directory.
php artisan make:action TestAction
The -d
or -m
parameters can be optionally specified to generate the action into a Domain
or Modules
directory. If both parameters are supplied, domain will take precedence.
The following command will generate a TestAction
into the app/Domain/Example/Actions
directory.
php artisan make:action TestAction -d Example
A DTO can be generated in the same way as an action, and supports both the -d
and -m
parameters.
php artisan make:dto TestData
A DTO can also be a collection of DTOs, which is handled automatically by Spatie's package. Spatie Generators will attempt to automatically decide the name of your target data object, to avoid any manual configuration.
php artisan make:dto TestDataCollection --collection
Will generate the following class:
<?php
namespace App\DTO;
use Spatie\DataTransferObject\DataTransferObjectCollection;
class TestDataCollection extends DataTransferObjectCollection
{
public function current(): TestData
{
return parent::current();
}
}