Skip to content

tkrause/versionable

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Versionable

Easy to use Model versioning for Laravel 4 and Laravel 5

image image image codecov.io Scrutinizer Code Quality Build Status

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

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

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.

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' ];

}

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;

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.

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();

Versionable is free software distributed under the terms of the MIT license.

About

Laravel 4 / 5 Model versioning made easy

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%