Skip to main content
Laravel Data uses PHP reflection to infer property types, validation rules, and transformations. While powerful, this comes with a performance cost that can be eliminated by caching.

The Performance Cost

During development, the reflection overhead is typically negligible. However, in production with many data objects, this analysis happens on every request unless cached.
The good news: Laravel Data can operate without reflection by using cached analysis results.

Caching Structures

Cache all data object analysis before deploying to production:
This command:
  1. Searches for all data objects in your application
  2. Analyzes each data object
  3. Stores the results in your configured cache
Always run this command after creating or modifying data objects, or when deploying to production.

Configuration

Configure caching in config/data.php:

Cache Store

Specify which cache driver to use:
By default, it uses your application’s default cache store.

Discovery Directories

Configure which directories to search:
By default, the app/Data directory is searched recursively.

Discovery Method

The package uses php-structure-discoverer to find data classes.

Reflection Discovery (Default)

Enabled by default:

PHP Parser Discovery

Disable reflection and use the PHP parser instead:
When using the parser, include the Laravel Data source:
The parser can’t depend on reflection, so it needs to analyze the Laravel Data source code to understand what constitutes a data object.

Disabling Caching

Disable structure caching entirely:

Testing

The cache is automatically disabled during tests to ensure analysis results are always up-to-date and cache mocks aren’t affected.

Deployment Workflow

Recommended workflow for production deployments:

Viewing Cached Classes

See which classes were cached:
Output:

Performance Impact

Without Caching

Reflection analysis on every request~5-10ms per data class (depending on complexity)

With Caching

Pre-analyzed structures from cache~0.1-0.5ms per data class
For applications with 50+ data objects, caching can reduce data-related overhead by 90%+.