> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/spatie/laravel-data/llms.txt
> Use this file to discover all available pages before exploring further.

# Commands

> Artisan commands for generating and caching Laravel Data objects

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

## make:data

Generate new data objects quickly.

### Basic Usage

```bash theme={null}
php artisan make:data PostData
```

Creates `App\Data\PostData` in the `app/Data` directory.

### Custom Namespace

Change the namespace:

```bash theme={null}
php artisan make:data PostData --namespace=DataTransferObjects
```

Creates `App\DataTransferObjects\PostData` in `app/DataTransferObjects`.

### Options

| Option        | Short | Description                                           |
| ------------- | ----- | ----------------------------------------------------- |
| `--namespace` | `-N`  | The namespace (under \App) to place this Data class   |
| `--suffix`    | `-s`  | Suffix the class with this value                      |
| `--force`     | `-f`  | Create the Data class even if the file already exists |

### Examples

```bash theme={null}
# 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`:

```php theme={null}
'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:

```bash theme={null}
php artisan vendor:publish --tag=laravel-data-stubs
```

Edit the stub at `stubs/data.stub`:

```php theme={null}
<?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

```bash theme={null}
php artisan data:cache-structures
```

Output:

```
Caching data structures...
████████████████████ 100%

Cached 42 data classes
```

### Show Cached Classes

Display which classes were cached:

```bash theme={null}
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

```bash theme={null}
#!/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:

```bash theme={null}
php artisan data:cache-structures
```

Output:

```
Data structure caching is not enabled
```

Enable it in `config/data.php`:

```php theme={null}
'structure_caching' => [
    'enabled' => true,
    // ...
],
```

## Clear Cache

Clear the data structures cache:

```bash theme={null}
php artisan cache:clear
```

Or clear only data structures (if using a separate cache store):

```bash theme={null}
php artisan cache:forget laravel-data:*
```

<Note>
  The cache key prefix is configurable in `config/data.php` under `structure_caching.cache.prefix`.
</Note>
