Skip to content

Laravel Artisan Commands

Complete reference of all Laravel Artisan commands for SimpleDTO.

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

Create a new DTO class.

Terminal window
php artisan make:dto UserDTO

Creates 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,
) {}
}

Create a DTO with validation attributes:

Terminal window
php artisan make:dto CreateUserDTO --validation

Creates:

<?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,
) {}
}

Create a resource DTO for API responses:

Terminal window
php artisan make:dto UserResourceDTO --resource

Overwrite existing DTO:

Terminal window
php artisan make:dto UserDTO --force

Generate TypeScript types from DTOs.

Terminal window
php artisan dto:typescript

Generates TypeScript interfaces in resources/js/types/dtos.ts.

Specify output file:

Terminal window
php artisan dto:typescript --output=resources/js/types/api.ts

Watch for changes and regenerate automatically:

Terminal window
php artisan dto:typescript --watch

Specify export type:

Terminal window
php artisan dto:typescript --export=export # export interface
php artisan dto:typescript --export=declare # declare interface
php artisan dto:typescript --export= # no export

Migrate Spatie Laravel Data classes to SimpleDTO.

Terminal window
php artisan dto:migrate-spatie

Migrates all Spatie Data classes in app/Data directory.

Specify directory to scan:

Terminal window
php artisan dto:migrate-spatie --path=app/Data/Api

Preview changes without modifying files:

Terminal window
php artisan dto:migrate-spatie --dry-run

Create backup files before migration:

Terminal window
php artisan dto:migrate-spatie --backup

This creates .backup files for each migrated file.

Skip confirmation prompt:

Terminal window
php artisan dto:migrate-spatie --force

The command automatically:

  1. Finds all Spatie Data classes
  2. Replaces Data with SimpleDTO base class
  3. Updates namespace imports
  4. Adds readonly to properties
  5. Updates attribute namespaces
  6. Replaces WithCast with Cast attribute

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

List all DTOs in the project.

Terminal window
php artisan dto:list

Output:

+------------------+------------------+------------+
| DTO | Namespace | Properties |
+------------------+------------------+------------+
| UserDTO | App\DTO | 5 |
| CreateUserDTO | App\DTO\Requests | 3 |
| UserResourceDTO | App\DTO\Resources| 7 |
+------------------+------------------+------------+

Filter by namespace:

Terminal window
php artisan dto:list --namespace=App\\DTO\\Api

Validate DTO structure and configuration.

Terminal window
php artisan dto:validate UserDTO

Checks:

  • Class exists
  • Extends SimpleDTO
  • Properties are readonly
  • Validation attributes are valid
  • Type casts are valid
  • No circular dependencies

Validate all DTOs:

Terminal window
php artisan dto:validate --all

Attempt to fix common issues:

Terminal window
php artisan dto:validate UserDTO --fix

Cache validation rules for better performance.

Terminal window
php artisan dto:cache

Caches validation rules for all DTOs, improving validation performance by up to 198x.

Clear cache before caching:

Terminal window
php artisan dto:cache --clear

Clear validation rule cache.

Terminal window
php artisan dto:clear

Clears all cached validation rules.

Terminal window
# Create request DTO
php artisan make:dto CreateUserDTO --validation
# Create response DTO
php artisan make:dto UserResourceDTO --resource
# Generate TypeScript
php artisan dto:typescript
Terminal window
# Create DTO
php artisan make:dto OrderDTO
# Validate structure
php artisan dto:validate OrderDTO
# Generate TypeScript with watch
php artisan dto:typescript --watch
Terminal window
# Validate all DTOs
php artisan dto:validate --all
# Check TypeScript is up to date
php artisan dto:typescript --check
# Cache validation rules
php artisan dto:cache
Terminal window
# Clear old cache
php artisan dto:clear
# Cache validation rules
php artisan dto:cache
# Generate TypeScript
php artisan dto:typescript
Terminal window
php artisan make:dto UserDTO && php artisan dto:validate UserDTO
Terminal window
php artisan dto:typescript --watch &
Terminal window
# Always validate after creating DTOs
php artisan make:dto UserDTO
php artisan dto:validate UserDTO
Terminal window
# Add to deployment script
php artisan dto:cache
Terminal window
# Add to composer.json scripts
"scripts": {
"post-autoload-dump": [
"@php artisan dto:typescript"
]
}
Terminal window
# Add to CI/CD pipeline
php artisan dto:validate --all
php artisan dto:typescript --check