Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Data

Laravel Data Laravel Package

spatie/laravel-data

Define rich, typed data objects once and use them for requests, validation, API resources/transformers, and TypeScript definitions. Create from arrays/requests/models, apply rules automatically, and transform only what’s needed with lazy properties.

View on GitHub
Deep Wiki
Context7

spatie/laravel-data lets you define rich, typed data objects once and reuse them across your Laravel app. Create a class extending Data and get consistent casting, transformation, and validation without duplicating logic in form requests, transformers, or DTOs.

Use Data objects as a single source of truth for input and output, making APIs and front-ends easier to keep in sync.

  • Transform Data objects into API resources automatically
  • Create & validate Data from request input, with inferred rules
  • Lazy properties to include only requested fields/relations
  • Custom casting/creation from arbitrary input types
  • Generate TypeScript definitions from your Data classes
Frequently asked questions about Laravel Data
How does laravel-data compare to Laravel’s built-in Form Requests and API Resources?
Laravel-data unifies validation and serialization into a single typed object, reducing redundancy. Unlike Form Requests (validation-only) or API Resources (serialization-only), it handles both while adding TypeScript generation and lazy properties for partial transforms. It’s ideal for complex APIs where data contracts need strict consistency across layers.
Can I incrementally adopt laravel-data without rewriting existing Form Requests or API Resources?
Yes. Start by replacing new features or non-critical endpoints. The package is designed to coexist with existing Laravel components. For validation, migrate rules incrementally by defining them in data objects. Serialization can be adopted per resource, avoiding a full rewrite.
Does laravel-data support custom validation logic beyond Laravel’s built-in rules?
Absolutely. You can use custom validation logic via the `rules()` method or by extending the `Data` class. For complex cases, leverage Laravel’s validation rules or create custom rules within the data object’s constructor or methods. The package also supports custom casts for transforming data before validation.
What’s the performance impact of using reflection for data object analysis?
Reflection adds overhead during development, but the package mitigates this with caching via `php artisan data:cache-structures`. In production, this command pre-compiles data object structures, making runtime performance negligible. For serverless or CI environments, cache structures early in the pipeline.
How does TypeScript generation work, and can it handle API versioning?
The package auto-generates TypeScript interfaces from your data objects, reducing manual effort. For API versioning, you’ll need to manage TypeScript files manually (e.g., by versioning the generated files or using a build script). The generated types reflect your PHP data objects, so breaking changes will require updates to both backend and frontend.
Will laravel-data work with Laravel’s Eloquent models, or is it only for API responses?
It works with both. You can use data objects to validate input before saving to Eloquent models or to serialize model data for APIs. The package integrates with Eloquent’s casting system and supports lazy properties for partial transforms, making it versatile for both persistence and API layers.
Are there any limitations with lazy properties or partial transforms?
Lazy properties are useful for partial transforms but require explicit definition in your data object. They’re not automatically inferred, so you must mark properties with `#[Lazy]` or configure them manually. This ensures clarity but adds a small upfront cost for setup.
How does laravel-data handle nested data objects or relationships?
Nested data objects are fully supported. You can define relationships within data objects using other data classes, and the package will handle validation and serialization recursively. For Eloquent relationships, use the `resolveUsing()` method to transform related data into nested data objects.
What Laravel and PHP versions are required, and are there breaking changes between versions?
The package requires PHP 8.1+ and Laravel 9+. Version 4.x is stable, but breaking changes may occur in major updates. Always check the [changelog](https://github.com/spatie/laravel-data/blob/main/CHANGELOG.md) before upgrading. The team aims for backward compatibility but reserves the right to deprecate features.
Can I use laravel-data for GraphQL APIs, or is it REST-only?
While primarily designed for REST APIs, laravel-data can also work with GraphQL. Use it to define input types and validate GraphQL mutations/resolvers. For output types, leverage the serialization features to transform data objects into GraphQL responses. The TypeScript generation is especially useful for GraphQL schemas.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport