- How do I replace manual array-to-object hydration with `from($data)` in Laravel?
- Replace `new User($request->all())` with `User::from($request->all())`. The `from()` method uses reflection and `#[Describe]` attributes to auto-map, cast, and validate properties from arrays or JSON, eliminating manual assignment and defensive checks.
- Does this work with Laravel Eloquent models?
- Yes, but it’s designed for DTOs or value objects. For Eloquent, use `from()` on a base model trait or extend the package to auto-hydrate from database arrays. Avoid mixing with Eloquent’s built-in hydration to prevent conflicts.
- What Laravel versions and PHP requirements does this support?
- Requires PHP 8.1+ for attributes and union types. Officially supports Laravel 9+ (PHP 8.0+ may need polyfills). Check the [GitHub actions](https://github.com/zero-to-prod/data-model/actions) for compatibility updates.
- Can I use `#[Describe]` attributes with Laravel Validation?
- Yes. Map `#[Describe]` rules (e.g., `required`, `nullable`, `cast`) to Laravel’s `Validator` by extending the package or using a custom rule resolver. Example: `#[Describe(required: true)]` becomes `required|string` in validation.
- What’s the performance impact of reflection-based hydration?
- Reflection adds ~10–20ms per hydration for complex objects. Benchmark in your API’s hot paths. Cache `ReflectionAttribute` instances for repeated hydrations (e.g., API responses) or use OPcache/JIT compilation.
- How do I handle nested or recursive data structures?
- Use the `via` parameter in `#[Describe]` to specify custom hydrators for nested objects or arrays. Example: `#[Describe(via: [Address::class])]` auto-hydrates an `Address` object from a nested array. Test edge cases like circular references.
- Is this a drop-in replacement for `spatie/array-to-object`?
- Yes, but with stricter typing and fewer edge cases. Replace `spatie/array-to-object` by adding the `DataModel` trait and using `from($data)`. It enforces PHP types and `#[Describe]` rules upfront, reducing runtime errors.
- How do I handle API error responses when `from()` fails?
- Catch `PropertyRequiredException` or `InvalidCastException` and return HTTP 422 with validation-like errors. Extend Laravel’s `Validator` to reuse `#[Describe]` rules or surface exceptions as JSON: `return response()->json(['errors' => $e->getErrors()], 422).`
- Can I integrate this with Laravel’s `php artisan make:model`?
- Not natively, but you can create a custom artisan command or IDE plugin. Example: A `make:dto` command could auto-generate `#[Describe]` attributes for new classes. Contribute to the package or fork to add this feature.
- What’s the migration path for large Laravel codebases?
- Start with non-critical DTOs (e.g., API responses, form requests). Phase 1: Add `DataModel` to new classes; keep legacy constructors. Phase 2: Refactor services to use `from()`. Phase 3: Deprecate old constructors via `@deprecated` PHPDoc. Use feature flags for gradual adoption.