PHPackages                             olliecodes/eloquent-identity - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. olliecodes/eloquent-identity

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

olliecodes/eloquent-identity
============================

An identity map implementation for Eloquent

v1.2.1(4y ago)220MITPHPPHP ^7.4 | ^8.0

Since Jul 7Pushed 4y agoCompare

[ Source](https://github.com/olliecodes/eloquent-identity)[ Packagist](https://packagist.org/packages/olliecodes/eloquent-identity)[ RSS](/packages/olliecodes-eloquent-identity/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (1)Versions (5)Used By (0)

Eloquent Identity
=================

[](#eloquent-identity)

[![Latest Stable Version](https://camo.githubusercontent.com/48f76202cf6307c305e9c4f3a1483584b755400aeae00fa33a1bb39a7d66b036/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965636f6465732f656c6f7175656e742d6964656e746974792f762f737461626c652e706e67)](https://packagist.org/packages/olliecodes/eloquent-identity)[![Latest Unstable Version](https://camo.githubusercontent.com/ac15c98695b66dc2cd28e74b05da763ca1021a8ca9a206cb48918ad9eb4382f6/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965636f6465732f656c6f7175656e742d6964656e746974792f762f756e737461626c652e706e67)](https://packagist.org/packages/olliecodes/eloquent-identity)[![License](https://camo.githubusercontent.com/4343bde3e9d3bbbf1116fa1f2173321874e38647f3dc9d2c6b6f5bea3f61ca59/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965636f6465732f656c6f7175656e742d6964656e746974792f6c6963656e73652e706e67)](https://packagist.org/packages/olliecodes/eloquent-identity)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d37b5496e42b3bc996152066659ac836f8e2dddc107acadc6d2e064fe5ad5783/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f6c6c6965636f6465732f656c6f7175656e742d6964656e746974792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/olliecodes/eloquent-identity/?branch=master)

- **Laravel**: 7.\* || 8.\*
- **PHP**: 7.\* || 8.\*
- **License**: MIT
- **Author**: Ollie Read
- **Author Homepage**:

Eloquent identity provides a cache on top of Eloquent preventing multiple models being created for a single database row using the Identity Map design pattern ([P of EAA](https://martinfowler.com/eaaCatalog/identityMap.html) &amp; [Wikipedia](https://en.wikipedia.org/wiki/Identity_map_pattern)).

#### Table of Contents

[](#table-of-contents)

- [Installing](#installing)
- [Usage](#usage)
    - [Finding](#finding)
    - [Hydrating](#hydrating)
    - [Belongs To](#belongsto)
    - [Flushing](#flushing)
- [How does it work](#how)
- [Why?](#why)

Installing
----------

[](#installing)

To install this package simply run the following command.

```
composer require olliecodes/eloquent-identity

```

This package uses auto-discovery to register the service provider but if you'd rather do it manually, the service provider is:

```
OllieCodes\Eloquent\Identity\ServiceProvider

```

There is no configuration required.

Usage
-----

[](#usage)

To make use of the Eloquent identity map on your models, add the following trait.

```
OllieCodes\Eloquent\Identity\Concerns\MapsIdentity

```

### Finding

[](#finding)

Any calls to `find()`, `findOrFail()` or `findOrNew()` on a model that uses this trait, will skip the query if a model has already been created using the provided id.

Calls to `findMany()` will skip any ids that have already been used, only querying ones that are not present in the cache.

The query is only skipped if there are no where clauses, joins or having statements.

If you wish to force the query, you can call `refreshIdentityMap()` on the query builder instance. If you wish to skip the query on a builder instance where `refreshIdentityMap()` has been called, you can call `useIdentityMap()`.

### Hydrating

[](#hydrating)

When the query builder attempts to create a new instance of a model using this trait, with a key that matches an already existing instance of the model, the existing instance will be used.

If the model is using timestamps, and the returned attributes are newer, the attributes on the existing instance will be updated, but will retain any changes you'd previously made.

### BelongsTo

[](#belongsto)

If a belongs to relationship is loaded (not belongs to many) without constraints and without `refreshIdentityMap()` being called, the query will skip any model instances that already exist.

### Flushing

[](#flushing)

If you wish to flush the cached models, call `flushIdentities()` on an instance of `IdentityManager`, or on the `Identity`facade.

How
---

[](#how)

The `IdentityManager` stores an array containing all existing model instances and their identity.

The identities for models are stored as string, created using the following class.

```
OllieCodes\Eloquent\Identity\ModelIdentity

```

This contains a key, the model class name, and the connection name. The string version of these looks like so:

```
connection:class:key

```

Why
---

[](#why)

It's very easy to end up with multiple versions of the same model, meaning that updates on one aren't persisted to others.

Eloquent identity was created to reduce the number of models created, help limit unnecessary queries, and allow for consistent model interaction. It doesn't matter where in your code you're dealing with user 1, any changes made during a request will persist across all instances.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 94.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~166 days

Total

4

Last Release

1634d ago

PHP version history (2 changes)v1.0.0PHP ^7.4

v1.2.0PHP ^7.4 | ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/662649a96637829fc96fda162ef14e9f656b09ea8d696f37156e0da01cd5fa43?d=identicon)[ollieread](/maintainers/ollieread)

---

Top Contributors

[![ollieread](https://avatars.githubusercontent.com/u/469515?v=4)](https://github.com/ollieread "ollieread (18 commits)")[![MrMage](https://avatars.githubusercontent.com/u/117466?v=4)](https://github.com/MrMage "MrMage (1 commits)")

---

Tags

laravelormeloquentIdentity map

### Embed Badge

![Health badge](/badges/olliecodes-eloquent-identity/health.svg)

```
[![Health](https://phpackages.com/badges/olliecodes-eloquent-identity/health.svg)](https://phpackages.com/packages/olliecodes-eloquent-identity)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[mostafaznv/laracache

LaraCache is a customizable cache trait to cache queries on model's events

27246.8k1](/packages/mostafaznv-laracache)[wayofdev/laravel-cycle-orm-adapter

🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.

3516.7k3](/packages/wayofdev-laravel-cycle-orm-adapter)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)[mostafaznv/nova-laracache

LaraCache Tool for Laravel Nova

103.8k](/packages/mostafaznv-nova-laracache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
