937 B
937 B
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
createdevent - Deleted models removed via
deleting/forceDeletingevents - Subsequent queries update existing instances rather than creating duplicates
#laravel #eloquent #pattern #identity-map #orm #caching #memory