Skip to main content

Installation via Composer

You can install Laravel Data using Composer:
composer require spatie/laravel-data
Laravel’s package auto-discovery will automatically register the service provider, so no manual registration is needed.

Publishing the configuration

The package works out of the box with sensible defaults. However, you can optionally publish the configuration file to customize behavior:
php artisan vendor:publish --provider="Spatie\LaravelData\LaravelDataServiceProvider" --tag="data-config"
This creates a config/data.php file in your application.

Configuration overview

The published configuration file allows you to customize:
Configure date formats and timezones for date transformations:
'date_format' => DATE_ATOM,
'date_timezone' => null,
Define transformers that convert complex types to simple types:
'transformers' => [
    DateTimeInterface::class => \Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer::class,
    \Illuminate\Contracts\Support\Arrayable::class => \Spatie\LaravelData\Transformers\ArrayableTransformer::class,
    BackedEnum::class => Spatie\LaravelData\Transformers\EnumTransformer::class,
],
Define casts that convert simple types to complex types:
'casts' => [
    DateTimeInterface::class => Spatie\LaravelData\Casts\DateTimeInterfaceCast::class,
    BackedEnum::class => Spatie\LaravelData\Casts\EnumCast::class,
],
Configure how validation rules are automatically inferred from property types:
'rule_inferrers' => [
    Spatie\LaravelData\RuleInferrers\SometimesRuleInferrer::class,
    Spatie\LaravelData\RuleInferrers\NullableRuleInferrer::class,
    Spatie\LaravelData\RuleInferrers\RequiredRuleInferrer::class,
    Spatie\LaravelData\RuleInferrers\BuiltInTypesRuleInferrer::class,
    Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
],
Enable caching for improved performance in production:
'structure_caching' => [
    'enabled' => true,
    'directories' => [app_path('Data')],
    'cache' => [
        'store' => env('CACHE_STORE', env('CACHE_DRIVER', 'file')),
        'prefix' => 'laravel-data',
        'duration' => null,
    ],
],
Control when data objects are validated:
'validation_strategy' => \Spatie\LaravelData\Support\Creation\ValidationStrategy::OnlyRequests->value,
Options:
  • OnlyRequests: Validate only when creating from requests (default)
  • Always: Validate on every creation
  • Disabled: Never validate automatically
Configure the make:data Artisan command:
'commands' => [
    'make' => [
        'namespace' => 'Data',
        'suffix' => 'Data',
    ],
],
Controls how data objects appear when using dump() or dd():
  • 'enabled' - Always use the custom caster (shows data properties clearly)
  • 'disabled' - Never use the custom caster (standard object dump)
  • 'development' (default) - Use custom caster only in non-production environments
'var_dumper_caster_mode' => 'development',

Creating data classes

You can quickly generate a data class using the Artisan command:
php artisan make:data Post
This creates app/Data/PostData.php:
<?php

namespace App\Data;

use Spatie\LaravelData\Data;

class PostData extends Data
{
    //
}
By default, data classes are created in the app/Data directory with a Data suffix. You can customize this in the configuration file or via command options.

Performance optimization

For production environments, ensure structure caching is enabled in config/data.php:
'structure_caching' => [
    'enabled' => true,
    'directories' => [app_path('Data')],
],
This caches reflection analysis of data objects, significantly improving performance.
If you add data classes outside of app/Data, add those directories to the structure_caching.directories array.

Next steps

Quickstart

Build your first data object

Requirements

Check system requirements