16 lines
937 B
Markdown
16 lines
937 B
Markdown
https://ollieread.com/articles/a-minimal-identity-map-for-laravel-eloquent
|
|
|
|
Implementation pattern (not a package) that ensures each database row has a unique representation in memory. Multiple queries for the same row return the identical object instance rather than separate copies.
|
|
|
|
## Problem Solved
|
|
Without an identity map, Eloquent creates multiple instances of the same database row. If you update a User model in one place, changes are not reflected in other places holding a different instance of the same row.
|
|
|
|
## How It Works
|
|
- Static registry stores model instances mapped by class name and primary key
|
|
- Hooks capture newly queried models via `newFromBuilder()`
|
|
- Freshly created models added via the `created` event
|
|
- Deleted models removed via `deleting`/`forceDeleting` events
|
|
- Subsequent queries update existing instances rather than creating duplicates
|
|
|
|
#laravel #eloquent #pattern #identity-map #orm #caching #memory
|