PHPackages                             pedrocruzlopez/versionable - 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. pedrocruzlopez/versionable

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

pedrocruzlopez/versionable
==========================

Fork from mpociot/versionable Allows to create Laravel 4 / 5 / 6 / 7 / 8 / 9 Model versioning and restoring

v4.4.5(2y ago)08MITPHPPHP ^7.3|^8.0

Since Sep 28Pushed 2y agoCompare

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

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

Versionable
===========

[](#versionable)

Laravel Model versioning made easy
----------------------------------

[](#laravel-model-versioning-made-easy)

[![image](https://camo.githubusercontent.com/f1d235d967844febf5e337b7daa79def17bb84802a24de12e4f714f44634fb55/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/f1d235d967844febf5e337b7daa79def17bb84802a24de12e4f714f44634fb55/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)[![image](https://camo.githubusercontent.com/c387c308ee1581988fed9ddda8d7bb9d7de61a9ba64ca7d29a62f2bba27f5d0b/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/c387c308ee1581988fed9ddda8d7bb9d7de61a9ba64ca7d29a62f2bba27f5d0b/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)[![image](https://camo.githubusercontent.com/653299180ae4a9c8c826eec218e0e9c4392fc639d45a9208848f782c53936639/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/653299180ae4a9c8c826eec218e0e9c4392fc639d45a9208848f782c53936639/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d706f63696f742f76657273696f6e61626c652e7376673f7374796c653d666c6174)[![codecov.io](https://camo.githubusercontent.com/b07780af1ba2b0f2a1451921e29e9b1a5484202c27532181b1ad6a0fbd2d1e3d/68747470733a2f2f636f6465636f762e696f2f6769746875622f6d706f63696f742f76657273696f6e61626c652f636f7665726167652e7376673f6272616e63683d6d6173746572)](https://codecov.io/github/mpociot/versionable?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/776b3ceff8ac53ad14b82097c541b95b856b54b44e2cd93a121379a800367f76/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d706f63696f742f76657273696f6e61626c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mpociot/versionable/?branch=master)[![Build Status](https://camo.githubusercontent.com/29479c440c77ae68f39d9edb54a40b45701f059a751f408eb85d7ad9755bb09f/68747470733a2f2f7472617669732d63692e6f72672f6d706f63696f742f76657273696f6e61626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mpociot/versionable)

Keep track of all your model changes and revert to previous versions of it.

```
// Restore to the previous change
$content->previousVersion()->revert();

// Get model from a version
$oldModel = Version::find(100)->getModel();
```

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

[](#installation)

You can install via composer:

```
composer require mpociot/versionable

```

Run the migrations.

```
php artisan migrate --path=vendor/mpociot/versionable/src/migrations

```

Alternatively, publish the migrations.

```
php artisan vendor:publish --provider="Mpociot\Versionable\Providers\ServiceProvider" --tag="migrations"

```

Then customize and run them.

```
php artisan migrate

```

Usage
-----

[](#usage)

Let the Models you want to set under version control use the `VersionableTrait`.

```
class Content extends Model {

	use Mpociot\Versionable\VersionableTrait;

}
```

That's it!

Every time you update your model, a new version containing the previous attributes will be stored in your database.

All timestamps and the optional soft-delete timestamp will be ignored.

### Exclude attributes from versioning

[](#exclude-attributes-from-versioning)

Sometimes you don't want to create a version *every* time an attribute on your model changes. For example your User model might have a `last_login_at` attribute. I'm pretty sure you don't want to create a new version of your User model every time that user logs in.

To exclude specific attributes from versioning, add a new array property to your model named `dontVersionFields`.

```
class User extends Model {

	use Mpociot\Versionable\VersionableTrait;

	/**
	 * @var array
	 */
	protected $dontVersionFields = [ 'last_login_at' ];

}
```

### Hidden fields

[](#hidden-fields)

There are times you might want to include hidden fields in the version data. You might have hidden the fields with the `visible` or `hidden` properties in your model.

You can have those fields that are typically hidden in the rest of your project saved in the version data by adding them to the `versionedHiddenFields` property of the versionable model.

```
class User {

    use VersionableTrait;

    // Typically hidden fields
    protected $hidden = ['email', 'password'];

    // Save these hidden fields
    protected $versionedHiddenFields = ['email', 'password'];

}
```

### Maximum number of stored versions

[](#maximum-number-of-stored-versions)

You can control the maximum number of stored versions per model. By default, there will be no limit and all versions will be saved. Depending on your application, this could lead to a lot of versions, so you might want to limit the amount of stored versions.

You can do this by setting a `$keepOldVersions` property on your versionable models:

```
class User {

    use VersionableTrait;

    // Keep the last 10 versions.
    protected $keepOldVersions = 10;

}
```

### Retrieving all versions associated to a model

[](#retrieving-all-versions-associated-to-a-model)

To retrieve all stored versions use the `versions` attribute on your model.

This attribute can also be accessed like any other Laravel relation, since it is a `MorphMany` relation.

```
$model->versions;
```

### Getting a diff of two versions

[](#getting-a-diff-of-two-versions)

If you want to know, what exactly has changed between two versions, use the version model's `diff` method.

The diff method takes a version model as an argument. This defines the version to diff against. If no version is provided, it will use the current version.

```
/**
 * Create a diff against the current version
 */
$diff = $page->previousVersion()->diff();

/**
 * Create a diff against a specific version
 */
$diff = $page->currentVersion()->diff( $version );
```

The result will be an associative array containing the attribute name as the key, and the different attribute value.

### Revert to a previous version

[](#revert-to-a-previous-version)

Saving versions is pretty cool, but the real benefit will be the ability to revert to a specific version.

There are multiple ways to do this.

**Revert to the previous version**

You can easily revert to the version prior to the currently active version using:

```
$content->previousVersion()->revert();
```

**Revert to a specific version ID**

You can also revert to a specific version ID of a model using:

```
$revertedModel = Version::find( $version_id )->revert();
```

### Disable versioning

[](#disable-versioning)

In some situations you might want to disable versioning a specific model completely for the current request.

You can do this by using the `disableVersioning` and `enableVersioning` methods on the versionable model.

```
$user = User::find(1);
$user->disableVersioning();

// This will not create a new version entry.
$user->update([
    'some_attribute' => 'changed value'
]);
```

### Use different version table

[](#use-different-version-table)

Some times we want to have models versions in differents tables. By default versions are stored in the table 'versions', defined in Mpociot\\Versionable\\Version::$table.

To use a different table to store version for some model we have to change the table name. To do so, create a model that extends Mpociot\\Versionable\\Version and set the $table property to another table name.

```
class MyModelVersion extends Version
{
    $table = 'mymodel_versions';
    // ...
}
```

In the model that you want it use this specific versions table, use the `VersionableTrait` Trait and add the property `$versionClass` with value the specific version model.

```
class MyModel extends Eloquent
{
    use VersionableTrait ;
    protected $versionClass = MyModelVersion::class ;
    // ...
}
```

And do not forget to create a migration for this versions table, exactly as the default versions table.

License
-------

[](#license)

Versionable is free software distributed under the terms of the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 57.8% 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 ~172 days

Total

5

Last Release

996d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/45a7e891ddc2eb33e2ab53c244ea91cf6dfaf228c27b23222413ebb7c9ba5407?d=identicon)[pedrocruzlopez](/maintainers/pedrocruzlopez)

---

Top Contributors

[![mpociot](https://avatars.githubusercontent.com/u/804684?v=4)](https://github.com/mpociot "mpociot (78 commits)")[![nonoesp](https://avatars.githubusercontent.com/u/1243210?v=4)](https://github.com/nonoesp "nonoesp (20 commits)")[![Cyrille37](https://avatars.githubusercontent.com/u/368965?v=4)](https://github.com/Cyrille37 "Cyrille37 (5 commits)")[![pedrocruzlopez](https://avatars.githubusercontent.com/u/12741167?v=4)](https://github.com/pedrocruzlopez "pedrocruzlopez (5 commits)")[![rumeau](https://avatars.githubusercontent.com/u/2340790?v=4)](https://github.com/rumeau "rumeau (4 commits)")[![derhofbauer](https://avatars.githubusercontent.com/u/22151619?v=4)](https://github.com/derhofbauer "derhofbauer (4 commits)")[![MarcelWeidum](https://avatars.githubusercontent.com/u/9413586?v=4)](https://github.com/MarcelWeidum "MarcelWeidum (2 commits)")[![pheeque](https://avatars.githubusercontent.com/u/988061?v=4)](https://github.com/pheeque "pheeque (2 commits)")[![emilv](https://avatars.githubusercontent.com/u/1684914?v=4)](https://github.com/emilv "emilv (2 commits)")[![oschettler](https://avatars.githubusercontent.com/u/124634?v=4)](https://github.com/oschettler "oschettler (2 commits)")[![coffe4u](https://avatars.githubusercontent.com/u/2783407?v=4)](https://github.com/coffe4u "coffe4u (1 commits)")[![yellowwilson](https://avatars.githubusercontent.com/u/40550201?v=4)](https://github.com/yellowwilson "yellowwilson (1 commits)")[![dubcanada](https://avatars.githubusercontent.com/u/120325?v=4)](https://github.com/dubcanada "dubcanada (1 commits)")[![fedeisas](https://avatars.githubusercontent.com/u/251675?v=4)](https://github.com/fedeisas "fedeisas (1 commits)")[![geoffbeaumont](https://avatars.githubusercontent.com/u/3082468?v=4)](https://github.com/geoffbeaumont "geoffbeaumont (1 commits)")[![maximal](https://avatars.githubusercontent.com/u/980679?v=4)](https://github.com/maximal "maximal (1 commits)")[![merlinblack](https://avatars.githubusercontent.com/u/144381?v=4)](https://github.com/merlinblack "merlinblack (1 commits)")[![michaelachrisco](https://avatars.githubusercontent.com/u/398491?v=4)](https://github.com/michaelachrisco "michaelachrisco (1 commits)")[![nhowell](https://avatars.githubusercontent.com/u/200475?v=4)](https://github.com/nhowell "nhowell (1 commits)")[![stojankukrika](https://avatars.githubusercontent.com/u/10199584?v=4)](https://github.com/stojankukrika "stojankukrika (1 commits)")

---

Tags

laravelmodelhistoryrestoreversionardent

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pedrocruzlopez-versionable/health.svg)

```
[![Health](https://phpackages.com/badges/pedrocruzlopez-versionable/health.svg)](https://phpackages.com/packages/pedrocruzlopez-versionable)
```

###  Alternatives

[mpociot/versionable

Allows to create Laravel 4 / 5 / 6 / 7 / 8 / 9 / 10 / 11 Model versioning and restoring

7841.1M6](/packages/mpociot-versionable)[venturecraft/revisionable

Keep a revision history for your models without thinking, created as a package for use with Laravel

2.6k6.6M51](/packages/venturecraft-revisionable)[panoscape/history

Eloquent model history tracking for Laravel

162130.3k](/packages/panoscape-history)

PHPackages © 2026

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