Download the PHP package brighten/immutable-model without Composer
On this page you can find all versions of the php package brighten/immutable-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package immutable-model
ImmutableModel
An Eloquent-compatible, read-only model kernel for Laravel 11+
ImmutableModel provides first-class, enforceable read-only models for Laravel applications. It's perfect for SQL views, read-only tables, denormalized projections, and as a CQRS read-side primitive.
What "Immutable" Means
ImmutableModel enforces database immutability, not strict object immutability:
| Operation | Allowed? | Example |
|---|---|---|
| Read from database | ✅ Yes | User::find(1), User::where(...)->get() |
| In-memory attribute changes | ✅ Yes | $user->computed_field = 'value' |
| Database persistence | ❌ Throws | $user->save(), $user->update(), $user->delete() |
| Static write methods | ❌ Throws | User::create(), User::insert() |
This design prevents accidental database writes while remaining compatible with common Laravel patterns like adding computed properties for API responses, serialization, and working with collections.
Why ImmutableModel?
- Enforce architectural boundaries: Prevent accidental database writes at the model level
- Eliminate persistence bugs: Any save/update/delete attempt throws immediately - no silent failures
- Improved performance: 47-74% faster hydration, 25-70% faster eager loading
- Lower memory footprint: ~41% less memory (~1 KB vs ~1.65 KB per model)
- Familiar API: Eloquent-compatible read semantics for easy adoption
- Laravel ecosystem compatible: Works with API Resources, serialization, and other common patterns
Installation
Requirements
- PHP 8.2+
- Laravel 11+
Quick Start
API Reference
Model Configuration
Querying
All standard Eloquent read operations are supported:
Relationships
Supported relationship types:
Casting
Full Eloquent casting support:
Custom casters must implement Illuminate\Contracts\Database\Eloquent\CastsAttributes. Only the get() method is called.
Collections
Query results return Laravel's standard Eloquent\Collection. All collection methods work normally - immutability is enforced on database operations, not on in-memory manipulation:
Pagination
Full pagination support:
Chunking & Lazy Loading
Global Scopes
Apply query constraints automatically using Laravel's native Scope interface:
Hydration from Raw Data
Create models from existing data without database queries:
Comparison: ImmutableModel vs Eloquent
| Feature | ImmutableModel | Eloquent |
|---|---|---|
| Read queries | Yes | Yes |
| Relationships | Yes | Yes |
| Eager loading | Yes | Yes |
| Attribute casting | Yes | Yes |
| Accessors | Yes | Yes |
| Pagination | Yes | Yes |
| Global scopes | Yes | Yes |
| Write operations | Throws | Yes |
| Dirty tracking | No | Yes |
| Events/Observers | No | Yes |
| Mutators | No | Yes |
| Timestamps | No | Yes |
| Mass assignment | No | Yes |
Performance
Benchmarks show ImmutableModel is significantly faster for read operations:
Hydration Speed
| Rows | Eloquent | ImmutableModel | Improvement |
|---|---|---|---|
| 100 | 0.30ms | 0.09ms | -70% |
| 1,000 | 3.09ms | 0.80ms | -74% |
| 10,000 | 34.27ms | 9.37ms | -73% |
| 100,000 | 447.29ms | 236.63ms | -47% |
Memory Usage
| Rows | Eloquent | ImmutableModel | Per Model (E) | Per Model (I) | Savings |
|---|---|---|---|---|---|
| 100 | 166 KB | 97 KB | 1.66 KB | 998 B | 41% |
| 1,000 | 1.61 MB | 973 KB | 1.65 KB | 996 B | 41% |
| 10,000 | 16.2 MB | 9.56 MB | 1.66 KB | 1003 B | 41% |
| 100,000 | 161.5 MB | 95.1 MB | 1.65 KB | 997 B | 41% |
Eager Loading (10 posts per user)
| Users | Models | Eloquent | Immutable | Time Δ | Eloquent Mem | Immutable Mem | Mem Δ |
|---|---|---|---|---|---|---|---|
| 10 | 110 | 1.68ms | 1.27ms | -25% | 184 KB | 105 KB | 43% |
| 100 | 1,100 | 5.75ms | 2.23ms | -61% | 1.76 MB | 1.02 MB | 42% |
| 1,000 | 11,000 | 64.22ms | 19.34ms | -70% | 17.53 MB | 10.23 MB | 42% |
Use Cases
ImmutableModel is ideal for:
- SQL Views: Represent database views as read-only models
- Read Replicas: Query read-only database replicas safely
- CQRS Read Models: Enforce read-side immutability in CQRS architectures
- Denormalized Projections: Work with pre-computed, read-only data
- API Responses: Build response data with computed fields, knowing it won't accidentally persist
- Architectural Boundaries: Enforce that certain models are never written to from application code
Not Intended For
- Models that need write operations
- Models using Eloquent events/observers
- Models requiring dirty tracking or timestamps
- Drop-in replacement for all Eloquent models
Exceptions
| Exception | When Thrown |
|---|---|
ImmutableModelViolationException |
Any database persistence attempt (save, update, delete, create, etc.) |
ImmutableModelConfigurationException |
Invalid model configuration |
Contributing
Contributions are welcome! Please ensure all tests pass before submitting a PR:
License
MIT License. See LICENSE for details.
All versions of immutable-model with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0