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.
Data class, reducing code duplication by ~30-50% for CRUD-heavy applications.// FormRequest + API Resource + DTO
class CreateUserRequest extends FormRequest { ... }
class UserResource extends JsonResource { ... }
class UserDTO { ... }
Use:
class UserData extends Data { ... } // Handles validation, serialization, and TypeScript gen
Data classes.string in PHP → string in TypeScript).API Development
Data objects for consistent serialization/deserialization.UserData::from($request->all()) auto-validates and maps to a typed object.Frontend-Backend Sync
Data classes to eliminate manual API contract maintenance.SongData class auto-generates songData.ts for React/Vue.Microservices & Packages
Data objects (e.g., OrderData shared across inventory/payment services).WithData trait to auto-convert models/requests to typed objects (e.g., User::first()->getData()).Validation Refactoring
FormRequest::rules() with type-hinted validation in Data classes.class UserData extends Data {
public function __construct(
public string $email,
public string $password, // Auto-validates against Laravel’s rules
) {}
}
Legacy System Modernization
Data classes (e.g., UserData::from($user)).✅ Your app has repetitive DTOs/Resources/Requests (e.g., >50% of your backend code is boilerplate). ✅ You need strict type safety between frontend/backend (e.g., GraphQL, REST, or SPAs). ✅ Your team struggles with validation errors due to manual rule management. ✅ You’re building a Laravel monolith or microservices with shared data contracts. ✅ You want to reduce technical debt by consolidating validation, serialization, and TypeScript gen.
❌ Your app is tiny (e.g., <5 endpoints) and doesn’t need DTOs.
❌ You’re using a non-Laravel framework (e.g., Symfony, Django).
❌ You need advanced nested mutations (consider Laravel Nova or GraphQL).
❌ Your team resists PHP attributes (though they’re optional—you can use methods instead).
❌ You’re locked into a strict MVC pattern with no room for DTOs (though WithData trait helps mitigate this).
"This package cuts backend development time by 30-50% for CRUD apps by eliminating repetitive DTOs, API Resources, and FormRequests. It also reduces bugs by enforcing type safety between frontend and backend—no more mismatched API contracts. For example, a
UserDataclass replaces 3 classes (Request + Resource + DTO) and auto-generates TypeScript types, saving 10+ hours per feature for our frontend/backend teams. The MIT license and Spatie’s track record (1757 stars) make it a low-risk, high-reward choice."
Key Metrics to Track Post-Adoption:
*"Laravel Data lets you define your data model once and reuse it everywhere:
- Validation: Type hints replace
FormRequest::rules().- Serialization: Auto-generates API responses (like
JsonResourcebut smarter).- Frontend Sync: Generates TypeScript interfaces from PHP classes.
- Flexibility: Works with Eloquent, API Resources, or raw arrays.
Example Workflow:
- Define
UserDatawith type hints → validation is automatic.- Use
UserData::from($request)in controllers → no manual validation.- Call
$userData->toArray()→ consistent API responses.- Generate
userData.ts→ frontend gets types for free.Why Not DIY?
- Saves hundreds of lines of boilerplate per app.
- Spatie’s package is battle-tested (used in production by 1.7K+ repos).
- Supports advanced use cases like custom mappers, transformers, and nested objects."*
For Architects:
OrderData once, use it across inventory/payment services.Data classes incrementally.For Frontend Teams:
SongData class in Laravel → songData.ts in React with zero manual work.Call to Action:
"Let’s pilot this on our
UsersandOrdersmodules. If it saves us 20 hours in the first sprint, we’ll roll it out app-wide. Spatie’s package is MIT-licensed, so we can customize it if needed."
How can I help you explore Laravel packages today?