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;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