Versionable
Easy to use Model versioning for Laravel 4 and Laravel 5
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();
Contents
Installation
In order to add Versionable to your project, just add
"mpociot/versionable": "~2.0"
to your composer.json. Then run composer install
or composer update
.
Or run composer require mpociot/versionable
if you prefere that.
Run the migrations to create the "versions" table that will hold all version information.
php artisan migrate --path=vendor/mpociot/versionable/src/migrations
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
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' ];
}
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
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
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();
License
Versionable is free software distributed under the terms of the MIT license.