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

Immutable Laravel Package

innmind/immutable

Innmind/Immutable provides persistent, immutable collections for PHP, including sequences, sets, maps, and streams. It offers a functional API with lazy operations, structural sharing, and type-safe wrappers for predictable, side-effect-free data manipulation.

Deep Wiki
Context7

Innmind/Immutable provides immutable, persistent data structures for PHP, letting you model collections and optional/error-prone values without shared mutable state. It includes sequences, sets, maps, strings, and functional types such as Maybe and Either, so transformations stay explicit and side-effect-free.

  • Persistent collections: every “mutation” returns a new value.
  • Rich functional API: map, filter, fold, group, and more.
  • Safe optional/error handling with Maybe and Either.
  • Psalm-friendly generic type annotations for safer code.
  • Composable, lightweight design for predictable PHP applications.
Frequently asked questions about Immutable
What is innmind/immutable used for in a Laravel application?
It provides immutable collections and functional data types such as maps, sequences, sets, and optional values. In Laravel it fits best at domain boundaries for value objects, DTOs, command and event payloads, read models, and config snapshots rather than replacing Eloquent models or Laravel collections. Operations return new structures instead of mutating state, which can make domain code easier to reason about. Start with a small module and add mapper or adapter classes.
Does innmind/immutable require a Laravel service provider or facade?
No. It is a PHP primitives-wrapper library, not a Laravel-specific package, so it should not need a service provider or facade. Install it via Composer and use the classes directly in your domain layer. Confirm autoloading and version constraints in a compatibility spike before adopting it. Keep framework integration in your own adapters.
Which PHP and Laravel versions does innmind/immutable support?
The provided context does not confirm a support matrix, so verify PHP and Laravel constraints on Packagist and the canonical repository. Check Composer metadata before installing and run a compatibility spike with your Laravel version, PHPUnit or Pest, and static analysis tools. Pin an exact version and lockfile entry. Do not assume framework support without testing.
How do I install innmind/immutable with Composer?
Use Composer require innmind/immutable, but first verify the canonical package name, repository, and release provenance because the repository is unknown in the assessment. Pin an exact version rather than a loose constraint and commit the lockfile. Test the install in a branch before merging. No Laravel-specific installation steps are expected.
Can innmind/immutable replace Laravel collections or Eloquent models?
No, it should not become a framework-wide replacement for Eloquent models or Laravel collections. Keep Eloquent and Laravel collections in framework-facing code and map to immutable objects at application boundaries. Use it tactically for DTOs, value objects, event payloads, and read models. Retrofitting the entire codebase is risky and adds object-wrapping overhead.
How does innmind/immutable serialize to JSON, database columns, and queue payloads?
The context does not specify serialization behavior, so test JSON encoding, Eloquent casts, and queue serialization explicitly. Add adapter or mapper classes and custom Eloquent casts where needed. Confirm behavior under API response formatting and long-running workers such as Octane. Do not assume native array or scalar behavior.
What are the performance concerns of using immutable wrappers in Laravel?
Object wrapping adds allocation and serialization overhead compared with native arrays and scalars. Benchmark hot paths, queue throughput, API serialization, and memory under Octane or long-running workers. Avoid excessive wrapping in high-volume collection transformations without measurement. Immutability can reduce state-related defects, but it is not free.
Is innmind/immutable safe for production use in Laravel?
Treat it as a tactical dependency with vendor risk because the repository is unknown, adoption is low, and maintainer and security posture are not visible. Pin versions, monitor advisories, assign an internal owner, and wrap it behind internal interfaces to limit blast radius. Pilot it in a low-risk module with tests and benchmarks before critical-path use. Maintain a fork or replacement contingency.
How should I test innmind/immutable value objects and DTOs in Laravel?
Write unit tests for equality, comparison semantics, serialization, and boundary mapping. Add integration tests for Eloquent casts, JSON responses, queue payloads, and validation where relevant. Use PHPUnit or Pest and include static analysis in your pipeline. Test under the same PHP and Laravel versions you deploy.
What are alternatives to innmind/immutable for immutable data in Laravel?
Native PHP 8.1+ readonly classes, enums, and simple value objects can cover many DTO and identifier use cases without a dependency. Laravel collections remain suitable for framework-facing mutable workflows. Compare build versus buy and use innmind/immutable only if its collections and optional types add enough value over native constructs. Also evaluate other maintained immutable or value-object libraries after due diligence.
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.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
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