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.
Data classes.Data classes for inter-service communication).Data objects (e.g., UserData, OrderData).Data classes (via spatie/laravel-graphql).Data objects for consistency.ArticleData) for frontend consumption.php-dto, symfony/serializer) with deep customizations.Data classes)."This package lets us define our app’s data structures once in PHP, then reuse them everywhere—APIs, forms, and even frontend TypeScript. It’s like having a single source of truth for all data, which cuts development time by 30% and slashes bugs from mismatched types. For example, if we update a User model, the API response, frontend form, and validation rules all stay in sync automatically. It’s a no-code solution for consistency, and it’s free to use."
Key Outcomes:
*"Laravel Data replaces three separate concerns—validation, API serialization, and frontend typing—with a single Data class. Here’s how it works:
public string $email), and the package auto-validates input.Data and use ->toArray() or ->toJson() for consistent API payloads.SongData → songData.ts).Example:
class UserData extends Data {
public function __construct(
public string $name,
public int $age,
public Email $email, // Custom castable type
) {}
}
Now UserData works as:
Why Now?
Migration Path:
User, Order).php artisan data:cache-structures)."How can I help you explore Laravel packages today?