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

Create rich, typed data objects for Laravel that replace form requests and API transformers. Automatically map from requests, validate with inferred rules, transform to resources (with lazy/partial fields), and generate TypeScript definitions from the same source.

View on GitHub
Deep Wiki
Context7

Create unified resources and data transfer objects

Frequently asked questions about Laravel Data
How do I replace a Laravel Form Request with a Data object?
Extend `Data` and define properties with type hints in the constructor. Use `fromRequest()` to validate and populate the object from incoming requests. For example, replace `CreatePostRequest` with `PostData::fromRequest($request)` to handle validation and data extraction in one step.
Can I use Data objects with Laravel API Resources?
Yes. Data objects can be transformed into API resources using `toArray()` or `toJson()`. For example, extend `JsonResource` and pass a Data object to its constructor. The package handles serialization automatically, reducing boilerplate.
What Laravel versions does spatie/laravel-data support?
The package supports Laravel 9.x and 10.x. Check the [GitHub repository](https://github.com/spatie/laravel-data) for the latest compatibility details, as minor updates may align with new Laravel releases.
How do lazy properties work in Data objects?
Lazy properties defer transformation until explicitly accessed. Use `Lazy::inertiaDeferred()` for Inertia.js or `Lazy::jsonDeferred()` for API responses. This reduces payload size and improves performance by loading only requested fields.
Does this package generate TypeScript types automatically?
Yes, use Spatie’s `typescript-transformer` to generate TypeScript interfaces from your Data objects. Run `php artisan data:generate-typescript` to create `.d.ts` files. This ensures frontend types stay in sync with your backend data structure.
Will Data objects impact performance in production?
Reflection-based analysis adds minimal overhead. Mitigate this by running `php artisan data:cache-structures` in production to cache metadata. For high-traffic apps, ensure the cache (e.g., Redis) is optimized for low latency.
Can I use Data objects with Eloquent models?
Absolutely. Cast model attributes to Data objects using `hasOne` or `morphTo` relationships. For example, define `$casts = ['metadata' => SongData::class]` in your model to automatically convert attributes to typed Data objects.
How do I test Data objects in Laravel?
Test Data objects like regular classes. Use `Data::from()` to create instances from arrays or models, then assert properties. For validation, pass invalid data and verify exceptions. Mock `typescript-transformer` if testing TypeScript generation.
Are there alternatives to spatie/laravel-data for typed data objects?
Yes, consider `spatie/laravel-arrayable` for simpler serialization or `symfony/serializer` for broader use cases. However, `laravel-data` uniquely combines validation, lazy loading, and TypeScript generation in a Laravel-native way.
How do I handle nested Data objects or relationships?
Define nested Data objects as properties in your class. For relationships, use `Data::from()` to convert associated models. For example, `public function __construct(public UserData $author) {}` automatically handles nested validation and serialization.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai