Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Hydrator Laravel Package

patchlevel/hydrator

Hydrate objects to arrays and back without calling constructors. Supports final and readonly classes, nested structures, enums, dates, lazy hydration, field renaming, and metadata caching with a developer-friendly, fully typed API.

View on GitHub
Deep Wiki
Context7

A library for seamless hydration of objects to arrays - and back again, optimized for developer experience and performance

Frequently asked questions about Hydrator
Is patchlevel/hydrator a Laravel package or a framework-agnostic PHP library?
It is a framework-agnostic PHP serialization and hydration library, not a Laravel package. There is no service provider, config file, facade, or Eloquent integration; it operates on plain PHP objects. In a Laravel app you can still bind a configured Hydrator instance as a singleton and inject it where needed.
How do I install patchlevel/hydrator in a Laravel project?
Install it with composer require patchlevel/hydrator. The core library requires PHP 8.1 or newer, and lazy hydration via PHP 8.4 lazy proxies requires PHP 8.4. There is no Laravel publish step or config file, so integration is just Composer plus any singleton binding you choose to add.
Does patchlevel/hydrator work with final, readonly, and promoted-property DTOs?
Yes. It hydrates objects without calling the constructor, which makes final classes, readonly classes, promoted properties, and deeply nested structures viable without factory boilerplate. Be aware that skipping the constructor also skips any invariant checks you put there, so only hydrate trusted or already-validated data.
How does patchlevel/hydrator handle enums, dates, collections, and nested objects?
It automatically resolves normalizers for enums, date types, collections, array shapes, and nested objects. You can extend behavior with pluggable guessers and extensions when you need project-specific normalization, such as money objects or strongly typed IDs. This is the main reason to use it instead of plain json_encode for boundary DTOs.
Can I rename or exclude properties when extracting an object to an array?
Yes. The library supports attribute-driven field mapping so you can rename or exclude fields. That lets you keep a stable external contract decoupled from internal property names. It is useful for API payloads, webhook shapes, and event serialization where naming should not leak implementation details.
How should I integrate patchlevel/hydrator with Laravel actions and Form Requests?
Keep validation in Form Requests and use the hydrator after validation, typically by binding a configured Hydrator instance as a singleton in a small service provider. Inject it into actions or controllers to map validated arrays into immutable DTOs. It does not do validation, rule inference, Eloquent model hydration, query-builder integration, or resource transformation, so those stay in Laravel-native layers.
Is it safe to hydrate raw request payloads with patchlevel/hydrator?
No, not directly. Constructor bypass means objects whose constructors enforce invariants, such as a non-negative amount, can be hydrated into an invalid state from untrusted array input. Hydrate only from trusted, typed sources, and validate upstream before calling the hydrator.
What is crypto-shredding in patchlevel/hydrator and what are the risks?
Crypto-shredding is per-subject key destruction used as an erasure mechanism for personal data, and the library documents safe usage of personal data with it. It implies field-level encryption with key custody that your team owns, so encryption, key derivation, storage, rotation, and deletion semantics become your responsibility. Treat it as an audit item and get legal and security review before marketing erasure guarantees.
Does patchlevel/hydrator support caching for performance?
Yes. It supports metadata caching with any PSR-6 or PSR-16 cache. That helps avoid repeated reflection and metadata resolution in hot paths. Pair it with lazy hydration on PHP 8.4 if you need to defer object construction.
What are the alternatives to patchlevel/hydrator and is it production-ready?
Common alternatives are spatie/laravel-data, Symfony Serializer, or hand-rolled fromArray and toArray methods on DTOs. patchlevel/hydrator is narrower and framework-agnostic, with useful constructor bypass, attribute mapping, normalizers, and crypto-shredding, but it has very low adoption and a recent 2.0 release, so expect API churn and own the edge cases. Pilot it in a bounded, non-critical context such as queue job payloads or cache DTOs before committing broadly, and note that its Stryker mutation-testing badge is a positive correctness signal for a small package.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core