- 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.