- Can I use laminas/laminas-hydrator in a Laravel application?
- Yes. laminas/laminas-hydrator is framework-agnostic and installs with Composer, so you can use it inside Laravel without a Laravel-specific bridge. It complements Eloquent, FormRequest, and JsonResource by standardising array-to-object and object-to-array mapping rather than replacing those tools.
- How do I install laminas/laminas-hydrator in Laravel?
- Run composer require laminas/laminas-hydrator; for the current major line use the ^5.0 constraint. Version 5 requires PHP 8.1+ and psr/container ^2.0, so check your app's platform and dependency resolution first. No service provider is required unless you choose to bind configured hydrators in Laravel's container.
- What PHP and Laravel versions does laminas-hydrator 5.0 support?
- Treat 5.0 as PHP 8.1+ only; the enum-based filter conditions imply that floor, and you should confirm it in composer.json before committing. It aligns better with Laravel 10 and 11 where PSR-11 2.x is available. Laravel 8 may drop from your support matrix if other dependencies still pin PSR-11 1.x.
- How do I convert an array to an object with Laminas Hydrator?
- Instantiate a hydrator, optionally attach naming strategies, filters, and strategies, then call hydrate($data, new YourDto()). The hydrator maps array keys to object properties according to its configuration. For repeatable Laravel code, define the hydrator once in a service provider or factory and inject it where needed.
- How do I extract an object to an array for a Laravel API response?
- Call extract($object) on a configured hydrator to get an array representation of the object. Use filters and extraction strategies to allow-list fields or transform values before the array reaches your response layer. JsonResource can still handle the final JSON shaping, while the hydrator owns the object-to-array mapping.
- What breaks when upgrading laminas-hydrator from 4.x to 5.0?
- The main breaks are typed signatures on StrategyInterface and HydratorInterface, integer FilterComposite condition constants replaced by an enum, and static properties converted to instance properties in hydrators. Module Manager support and HydratorProviderInterface were removed, psr/container ^2.0 is required, and SerializableStrategy needs laminas-serializer v4. Inventory custom strategies and hydrators first, because those extension points are most likely to fatal.
- Does laminas-hydrator support nested objects and collections?
- Yes. Nested mapping is handled through strategies, especially HydratorStrategy, which delegates a property to another hydrator for hydration or extraction. You can combine strategies where a property needs custom handling. This keeps nested DTO mapping explicit instead of burying it in ad-hoc array loops.
- Can I use Laminas Hydrator with Eloquent models instead of DTOs?
- It is technically possible, but hydrating Eloquent models directly can bypass or duplicate behaviour that fillable, casts, mutators, and model events already provide. The package is strongest when mapping API payloads to DTOs or bridging legacy array configuration into objects. If you do use it with models, keep the hydrator configuration narrow and test for unintended mass-assignment effects.
- How should I test Laravel DTO mapping built with laminas-hydrator?
- Unit test the hydrator configuration without touching the database: hydrate a representative array, assert the DTO state, extract it back, and compare the result. Add cases for naming strategies, filters, and any custom strategy, since v5's typed signatures make those extension points the most fragile part. Round-trip tests catch configuration drift better than testing hydrate and extract in isolation.
- What are alternatives to laminas-hydrator for Laravel DTO mapping?
- Common alternatives include hand-written mappers, spatie/laravel-data, symfony/serializer, and JsonResource-based shaping, each with different trade-offs in convention versus control. laminas-hydrator is a good fit when you want explicit, reusable hydration and extraction rules with naming strategies and filters. It is maintained under the Laminas project, but v5 is a backward-incompatible major, so pin constraints and read the migration guide before production use.