PHPackages                             sbarre/eloquent-versioned - 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. sbarre/eloquent-versioned

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

sbarre/eloquent-versioned
=========================

Add transparent versioning to Laravel 5.2's Eloquent ORM

0.1.2(10y ago)301.1k10[7 issues](https://github.com/sbarre/eloquent-versioned/issues)[2 PRs](https://github.com/sbarre/eloquent-versioned/pulls)MITPHPPHP &gt;=5.4

Since May 25Pushed 9y ago6 watchersCompare

[ Source](https://github.com/sbarre/eloquent-versioned)[ Packagist](https://packagist.org/packages/sbarre/eloquent-versioned)[ Docs](https://github.com/sbarre/eloquent-versioned)[ RSS](/packages/sbarre-eloquent-versioned/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (7)Used By (0)

Eloquent Versioned
==================

[](#eloquent-versioned)

Adds transparent versioning support to Laravel 5.2's Eloquent ORM.

**WARNING: This repository is currently super-duper experimental. I will gladly accept pull requests and issues, but you probably shouldn't use this in production, and the interfaces may change without notice (although major changes will bump the version).**

**It was also recently updated to bring global scopes in line with Laravel 5.2 so if you are not yet on 5.2, stick with release 0.0.7.**

When using this trait (and with a table that includes the required fields), saving your model will actually create a new row instead, and increment the version number.

Using global scopes, old versions are ignored in the standard ORM operations (selects, updates, deletes) and relations (hasOne, hasMany, belongsTo, etc).

The package also provides some special methods to include old versions in queries (or only query old versions) which can be useful for showing a model's history, or the like.

Installation
------------

[](#installation)

To add via Composer:

```
composer require sbarre/eloquent-versioned --no-dev

```

Use the `--no-dev` flag to avoid pulling down all the testing dependencies (like the *entire Laravel framework*).

Migrations
----------

[](#migrations)

Versioned models require that your database table contain 3 fields to handle the versioning.

If you are creating a new table, or if you are changing an existing table, include the following lines in the `up()` method of the migration:

```
$table->integer('model_id')->unsigned()->default(1);
$table->integer('version')->unsigned()->default(1);
$table->integer('is_current_version')->unsigned()->default(1);
$table->index('is_current_version');
$table->index('model_id');
$table->index('version');
```

If your migration was altering an existing table, you should include these lines in the `down()` method of your migration:

```
$table->dropColumn(['model_id','version','is_current_version']);
$table->dropIndex(['model_id','version','is_current_version']);
```

#### Caveats

[](#caveats)

If you change the constants in `EloquentVersioned\VersionedBuilder` to rename the columns, remember to change them in your migrations as well.

Usage
-----

[](#usage)

In your Eloquent model class, start by adding the `use` statement for the Trait:

```
use EloquentVersioned\Traits\Versioned;
```

When the trait boots it will apply the proper scope, and provides overrides on various Eloquent methods to support versioned records.

Once the trait is applied, you use your models as usual, with the standard queries behaving as usual.

```
$project = Project::create([
    'name' => 'Project Name',
    'description' => 'Project description goes here'
])->fresh();

print_r($project->toArray());
```

This would then output (for example):

```
Array
(
    [id] => 1
    [version] => 1
    [name] => Project Name
    [description] => Project description goes here
    [created_at] => 2015-05-24 17:16:05
    [updated_at] => 2015-05-24 17:16:05
)
```

The actual database row looks like this:

```
Array
(
    [id] => 1
    [model_id] => 1
    [version] => 1
    [is_current_version] => 1
    [name] => Updated project Name
    [description] => Project description goes here
    [created_at] => 2015-05-24 17:16:05
    [updated_at] => 2015-05-24 17:16:05
)
```

Then if you change the model and save:

```
$project->name = 'Updated project name';
$project->save();

print_r($project->toArray());
```

This would then output:

```
Array
(
    [id] => 1
    [version] => 2
    [name] => Updated project Name
    [description] => Project description goes here
    [created_at] => 2015-05-24 17:16:05
    [updated_at] => 2015-05-24 17:16:45
)
```

The model mutates the `model_id` column into `id`, and hides some of the version-specific columns. In reality this is actually the same database row that now looks like this:

```
Array
(
    [id] => 1
    [model_id] => 1
    [version] => 2
    [is_current_version] => 1
    [name] => Updated project Name
    [description] => Project description goes here
    [created_at] => 2015-05-24 17:16:05
    [updated_at] => 2015-05-24 17:16:45
)
```

While a new row is inserted to save our previous version, which now looks like this:

```
Array
(
    [id] => 2
    [model_id] => 1
    [version] => 1
    [is_current_version] => 0
    [name] => Project Name
    [description] => Project description goes here
    [created_at] => 2015-05-24 17:16:05
    [updated_at] => 2015-05-24 17:16:05
)
```

So the `is_current_version` property is what the global scope is applied against, limiting all select queries to only records where `is_current_version = 1`.

Calling `save()` on a model replicates the original version into a new row (with `is_current_version = 0`), then increments the `version_id` property on our current model, changes the appropriate timestamps, and saves it.

If you are making a very minor change to a model and you don't want to create a new version, you can call `saveMinor()` instead.

```
$project->saveMinor(); // doesn't create a new version
```

#### Methods for dealing with old versions

[](#methods-for-dealing-with-old-versions)

If you want to retrieve a list of all versions of a model (or include old versions in a bigger query):

```
$projectVersions = Project::withOldVersions()->find(1);
```

If run after our example above, this would return an array with 2 models.

You can also retrieve a list of *only* old models by using:

```
$oldVersions = Project::onlyOldVersions()->find(1);
```

Otherwise, the rest of Eloquent's ORM operations should work as usual, including the out-of-the-box relations.

#### Methods for moving through a model's versions

[](#methods-for-moving-through-a-models-versions)

If you want to navigate through all of model's versions, in a linked-list manner:

```
$current = Project::find(1);

$previous = $current->getPreviousModel();
$next = $previous->getNextModel();

// $next == $current
```

If you are at the most recent version, `getNextModel()` will return `null` and likewise if you are at the oldest version, `getPreviousModel()` will return `null`.

Support &amp; Roadmap
---------------------

[](#support--roadmap)

As indicated at the top, this package is still **very experimental** and is under active development. The current roadmap includes test coverage and more extensive real-world testing, so pull requests and issues are always welcome!

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.1% 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 ~67 days

Total

5

Last Release

3741d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f80b50614fc5965f735a2b75998f43e2271e2b58eefe915000afc799a2598e50?d=identicon)[sbarre](/maintainers/sbarre)

---

Top Contributors

[![sbarre](https://avatars.githubusercontent.com/u/104985?v=4)](https://github.com/sbarre "sbarre (52 commits)")[![Towerful](https://avatars.githubusercontent.com/u/2766601?v=4)](https://github.com/Towerful "Towerful (1 commits)")

---

Tags

eloquentlaravelphplaravelormeloquentversioning

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sbarre-eloquent-versioned/health.svg)

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

###  Alternatives

[proai/eloquent-versioning

An extension for the Eloquent ORM to support versioning.

7125.9k](/packages/proai-eloquent-versioning)[sofa/model-locking

Pseudo pessimistic model locking with broadcasted events for Laravel Eloquent ORM.

5048.0k](/packages/sofa-model-locking)[chocofamilyme/laravel-tarantool

A Tarantool based Eloquent ORM and Query builder for Laravel

182.3k](/packages/chocofamilyme-laravel-tarantool)[andreagroferreira/laravel-sync-tracker

A Laravel package for tracking entity synchronization status between systems

113.0k](/packages/andreagroferreira-laravel-sync-tracker)

PHPackages © 2026

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