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

Powerful data objects for Laravel

Frequently asked questions about Laravel Data
How does `spatie/laravel-data` replace FormRequests in Laravel?
Instead of extending `FormRequest`, you define a `Data` class with typed properties. The package automatically validates incoming requests against these types using Laravel’s validation rules (e.g., `#[Required]`, `#[MaxLength]`). No need to duplicate validation logic in controllers or requests.
Can I use Laravel Data with existing API Resources?
Yes. The package integrates seamlessly with Laravel’s API Resources. Extend your `Data` class and use it directly in `toArray()` or `toResponse()` methods. It even supports lazy-loading partial fields, reducing payload size for APIs.
Does this work with Laravel 10+ only, or older versions?
The package officially supports Laravel 10+. While it may work on older versions, it requires PHP 8.2+ (for named arguments, attributes, and strict types). If you’re on Laravel 9 or below, check the GitHub issues for compatibility notes or alternatives.
How do I generate TypeScript interfaces from my Data classes?
Run the `spatie:laravel-data:typescript` Artisan command. It scans your `Data` classes and generates `.d.ts` files in `resources/js/types`. These interfaces mirror your PHP types, ensuring type safety between Laravel and frontend frameworks like Inertia.js or Vue.
What’s the performance impact of using Data classes vs. FormRequests?
The package uses reflection for validation, which adds minor overhead (~5–10% in benchmarks). For most applications, this is negligible. If you’re building a high-throughput API, profile the impact with tools like Laravel Debugbar or Blackfire.
How do I handle nested or circular Data class dependencies?
Use the `#[WithoutValidation]` attribute or the `withoutValidation()` method to exclude properties from validation. For circular references (e.g., `UserData` containing `PostData` which references `UserData`), explicitly configure the relationship in your `Data` class constructor or use lazy properties.
Can I migrate existing FormRequests to Data classes incrementally?
Yes. Start by creating `Data` classes for new features, then gradually replace FormRequests in controllers. Use the `rules()` method in `Data` classes to override or extend existing validation logic from FormRequests. Tests may need updates to mock the new structure.
Does this package work with Livewire or only Inertia.js?
It works with both. For Livewire, use the generated TypeScript interfaces in your component props or state. The package doesn’t enforce Livewire-specific logic but ensures type safety. Inertia.js benefits directly from auto-generated TypeScript for page props and API responses.
How do I test Data classes in Laravel’s testing framework?
Test Data classes like any other PHP object. Use factories or builders to create instances, then assert properties or validation errors. For HTTP tests, pass request data to `Data::fromRequest()` and verify the resulting object. Mock dependencies (e.g., Eloquent models) as needed.
Are there alternatives to `spatie/laravel-data` for DTOs in Laravel?
Yes. Consider `symfony/ux-datagrid` (for complex grids), `rector/rector` (for refactoring to DTOs), or `spatie/fractal` (for API resources). However, `laravel-data` uniquely combines validation, transformation, and TypeScript generation in one package, reducing boilerplate for full-stack Laravel apps.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony