Laravel Artisan Commands
Complete reference of all Laravel Artisan commands for SimpleDTO.
Introduction
Section titled “Introduction”SimpleDTO provides several Artisan commands for Laravel:
- make:dto - Create new DTOs
- dto:typescript - Generate TypeScript types
- dto:migrate-spatie - Migrate Spatie Laravel Data to SimpleDTO
- dto:list - List all DTOs
- dto:validate - Validate DTO structure
- dto:cache - Cache validation rules
- dto:clear - Clear validation cache
make:dto
Section titled “make:dto”Create a new DTO class.
Basic Usage
Section titled “Basic Usage”php artisan make:dto UserDTOCreates app/DTO/UserDTO.php:
<?php
namespace App\DTO;
use event4u\DataHelpers\SimpleDTO;
class UserDTO extends SimpleDTO{ public function __construct( public readonly int $id, public readonly string $name, ) {}}Options
Section titled “Options”—validation
Section titled “—validation”Create a DTO with validation attributes:
php artisan make:dto CreateUserDTO --validationCreates:
<?php
namespace App\DTO;
use event4u\DataHelpers\SimpleDTO;use event4u\DataHelpers\SimpleDTO\Attributes\Required;use event4u\DataHelpers\SimpleDTO\Attributes\Email;
class CreateUserDTO extends SimpleDTO{ public function __construct( #[Required] public readonly string $name,
#[Required, Email] public readonly string $email, ) {}}—resource
Section titled “—resource”Create a resource DTO for API responses:
php artisan make:dto UserResourceDTO --resource—force
Section titled “—force”Overwrite existing DTO:
php artisan make:dto UserDTO --forcedto:typescript
Section titled “dto:typescript”Generate TypeScript types from DTOs.
Basic Usage
Section titled “Basic Usage”php artisan dto:typescriptGenerates TypeScript interfaces in resources/js/types/dtos.ts.
Options
Section titled “Options”—output
Section titled “—output”Specify output file:
php artisan dto:typescript --output=resources/js/types/api.ts—watch
Section titled “—watch”Watch for changes and regenerate automatically:
php artisan dto:typescript --watch—export
Section titled “—export”Specify export type:
php artisan dto:typescript --export=export # export interfacephp artisan dto:typescript --export=declare # declare interfacephp artisan dto:typescript --export= # no exportdto:migrate-spatie
Section titled “dto:migrate-spatie”Migrate Spatie Laravel Data classes to SimpleDTO.
Basic Usage
Section titled “Basic Usage”php artisan dto:migrate-spatieMigrates all Spatie Data classes in app/Data directory.
Options
Section titled “Options”Specify directory to scan:
php artisan dto:migrate-spatie --path=app/Data/Api—dry-run
Section titled “—dry-run”Preview changes without modifying files:
php artisan dto:migrate-spatie --dry-run—backup
Section titled “—backup”Create backup files before migration:
php artisan dto:migrate-spatie --backupThis creates .backup files for each migrated file.
—force
Section titled “—force”Skip confirmation prompt:
php artisan dto:migrate-spatie --forceWhat it does
Section titled “What it does”The command automatically:
- Finds all Spatie Data classes
- Replaces
DatawithSimpleDTObase class - Updates namespace imports
- Adds
readonlyto properties - Updates attribute namespaces
- Replaces
WithCastwithCastattribute
Example
Section titled “Example”Before migration:
use Spatie\LaravelData\Data;use Spatie\LaravelData\Attributes\Validation\Required;
class UserData extends Data{ public function __construct( #[Required] public string $name, public string $email, ) {}}After migration:
use event4u\DataHelpers\SimpleDTO\SimpleDTO;use event4u\DataHelpers\SimpleDTO\Attributes\Required;
class UserData extends SimpleDTO{ public function __construct( #[Required] public readonly string $name, public readonly string $email, ) {}}See also: Migration from Spatie - Complete migration guide
dto:list
Section titled “dto:list”List all DTOs in the project.
Basic Usage
Section titled “Basic Usage”php artisan dto:listOutput:
+------------------+------------------+------------+| DTO | Namespace | Properties |+------------------+------------------+------------+| UserDTO | App\DTO | 5 || CreateUserDTO | App\DTO\Requests | 3 || UserResourceDTO | App\DTO\Resources| 7 |+------------------+------------------+------------+Options
Section titled “Options”—namespace
Section titled “—namespace”Filter by namespace:
php artisan dto:list --namespace=App\\DTO\\Apidto:validate
Section titled “dto:validate”Validate DTO structure and configuration.
Basic Usage
Section titled “Basic Usage”php artisan dto:validate UserDTOChecks:
- Class exists
- Extends SimpleDTO
- Properties are readonly
- Validation attributes are valid
- Type casts are valid
- No circular dependencies
Options
Section titled “Options”Validate all DTOs:
php artisan dto:validate --allAttempt to fix common issues:
php artisan dto:validate UserDTO --fixdto:cache
Section titled “dto:cache”Cache validation rules for better performance.
Basic Usage
Section titled “Basic Usage”php artisan dto:cacheCaches validation rules for all DTOs, improving validation performance by up to 198x.
Options
Section titled “Options”—clear
Section titled “—clear”Clear cache before caching:
php artisan dto:cache --cleardto:clear
Section titled “dto:clear”Clear validation rule cache.
Basic Usage
Section titled “Basic Usage”php artisan dto:clearClears all cached validation rules.
Real-World Examples
Section titled “Real-World Examples”Create API DTOs
Section titled “Create API DTOs”# Create request DTOphp artisan make:dto CreateUserDTO --validation
# Create response DTOphp artisan make:dto UserResourceDTO --resource
# Generate TypeScriptphp artisan dto:typescriptDevelopment Workflow
Section titled “Development Workflow”# Create DTOphp artisan make:dto OrderDTO
# Validate structurephp artisan dto:validate OrderDTO
# Generate TypeScript with watchphp artisan dto:typescript --watchCI/CD Pipeline
Section titled “CI/CD Pipeline”# Validate all DTOsphp artisan dto:validate --all
# Check TypeScript is up to datephp artisan dto:typescript --check
# Cache validation rulesphp artisan dto:cacheDeployment
Section titled “Deployment”# Clear old cachephp artisan dto:clear
# Cache validation rulesphp artisan dto:cache
# Generate TypeScriptphp artisan dto:typescriptCombining Commands
Section titled “Combining Commands”Create and Validate
Section titled “Create and Validate”php artisan make:dto UserDTO && php artisan dto:validate UserDTOGenerate TypeScript and Watch
Section titled “Generate TypeScript and Watch”php artisan dto:typescript --watch &Best Practices
Section titled “Best Practices”1. Use Validation in Development
Section titled “1. Use Validation in Development”# Always validate after creating DTOsphp artisan make:dto UserDTOphp artisan dto:validate UserDTO2. Cache in Production
Section titled “2. Cache in Production”# Add to deployment scriptphp artisan dto:cache3. Generate TypeScript Automatically
Section titled “3. Generate TypeScript Automatically”# Add to composer.json scripts"scripts": { "post-autoload-dump": [ "@php artisan dto:typescript" ]}4. Validate in CI/CD
Section titled “4. Validate in CI/CD”# Add to CI/CD pipelinephp artisan dto:validate --allphp artisan dto:typescript --checkSee Also
Section titled “See Also”- Laravel Integration - Laravel integration guide
- TypeScript Generation - TypeScript generation details
- Validation - Validation guide