Skip to main content
Laravel Data provides Artisan commands to streamline development and optimize production performance.

make:data

Generate new data objects quickly.

Basic Usage

php artisan make:data PostData
Creates App\Data\PostData in the app/Data directory.

Custom Namespace

Change the namespace:
php artisan make:data PostData --namespace=DataTransferObjects
Creates App\DataTransferObjects\PostData in app/DataTransferObjects.

Options

OptionShortDescription
--namespace-NThe namespace (under \App) to place this Data class
--suffix-sSuffix the class with this value
--force-fCreate the Data class even if the file already exists

Examples

# Custom namespace
php artisan make:data UserData --namespace=DTOs

# Custom suffix
php artisan make:data User --suffix=DTO

# Force overwrite
php artisan make:data PostData --force

Configuration

Customize default behavior in config/data.php:
'commands' => [
    'make' => [
        /*
         * The default namespace for generated Data classes. This exists under the application's root namespace,
         * so the default 'Data` will end up as '\App\Data', and generated Data classes will be placed in the
         * app/Data/ folder.
         */
        'namespace' => 'Data',
        
        /*
         * This suffix will be appended to all data classes generated by make:data, so that they are less likely
         * to conflict with other related classes, controllers or models with a similar name without resorting
         * to adding an alias for the Data object. Set to a blank string (not null) to disable.
         */
        'suffix' => 'Data',
    ],
]

Custom Stub

Publish and customize the stub file:
php artisan vendor:publish --tag=laravel-data-stubs
Edit the stub at stubs/data.stub:
<?php

namespace {{ namespace }};

use Spatie\LaravelData\Data;

class {{ class }} extends Data
{
    public function __construct(
        // Add your properties here
    ) {
    }
}

data:cache-structures

Cache data object analysis for production performance.

Basic Usage

php artisan data:cache-structures
Output:
Caching data structures...
████████████████████ 100%

Cached 42 data classes

Show Cached Classes

Display which classes were cached:
php artisan data:cache-structures --show-classes
Output:
Caching data structures...
████████████████████ 100%

Cached 42 data classes

+----------------------------------+
| Data Class                       |
+----------------------------------+
| App\Data\UserData                |
| App\Data\PostData                |
| App\Data\CommentData             |
| App\Data\CategoryData            |
| ...                              |
+----------------------------------+

When to Run

Run this command:
  1. Before deploying to production
  2. After creating new data objects
  3. After modifying existing data objects
  4. As part of your deployment script

Deployment Script Example

#!/bin/bash

# Pull latest code
git pull origin main

# Install dependencies
composer install --no-dev --optimize-autoloader

# Cache everything
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan data:cache-structures

# Restart services
php artisan queue:restart

Error Handling

If caching is disabled in config:
php artisan data:cache-structures
Output:
Data structure caching is not enabled
Enable it in config/data.php:
'structure_caching' => [
    'enabled' => true,
    // ...
],

Clear Cache

Clear the data structures cache:
php artisan cache:clear
Or clear only data structures (if using a separate cache store):
php artisan cache:forget laravel-data:*
The cache key prefix is configurable in config/data.php under structure_caching.cache.prefix.

Build docs developers (and LLMs) love