Laravel Versioned
A Laravel 5 package to handle versioning on any of your Eloquent models., (*1)
Example Usage
$post = new App\Post();
$post->title = 'Some title';
$post->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$post->save();
echo $post->getCurrentVersion(); // 1
The post is saved. There are no previous versions., (*2)
Now we can update the post., (*3)
$post->body = 'New body text';
$post->save();
echo $post->getCurrentVersion(); // 2
We've automatically versioned it., (*4)
$post->body = 'Even newer body text';
$post->save();
echo $post->getCurrentVersion(); // 3
And again., (*5)
$post->restoreVersion(1); // true
echo $post->getCurrentVersion(); // 4
$post->toArray();
[
"title" => "Some title",
"body" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
"updated_at" => "2015-09-15 19:43:45",
"created_at" => "2015-09-15 19:41:17",
"id" => 1,
]
Now we've restored the data as it was at version 1, but as a new version., (*6)
$post->rollback();
echo $post->getCurrentVersion(); // 3
$post->toArray();
[
"title" => "Some title",
"body" => "Even newer body text",
"updated_at" => "2015-09-15 19:43:45",
"created_at" => "2015-09-15 19:41:17",
"id" => 1,
]
Rollback is effectively an 'undo' on the model. It also removes the history so we're back at version 3., (*7)
Installation
Install with Composer:, (*8)
composer require nexuspoint/versioned
Then create the following migration:, (*9)
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVersionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('versions', function (Blueprint $table) {
$table->increments('id');
$table->integer('version_no')->unsigned();
$table->integer('subject_id')->unsigned();
$table->string('subject_class');
$table->string('name');
$table->text('data');
$table->string('hash');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('versions');
}
}
run migrations, (*10)
php artisan migrate
Add the trait to your Eloquent class, and set $versionNameColumn to the field that you want to record as the version name column., (*11)
<?php
use Illuminate\Database\Eloquent\Model;
use NexusPoint\Versioned\Versioned;
class Post extends Model
{
use Versioned;
/**
* Field from the model to use as the versions name
* @var string
*/
protected $versionNameColumn = 'title';
}
Other methods
Manually add a new version of the model in its current state. I recommend to save the model after this. Normally you wouldn't need to call this manualy as the model will automatically version every time it is changed., (*12)
$post->addVersion('optional version name');
Get all versions for display in UI to select a version, (*13)
$post->getAllVersions();
Get a specific version. This method will return the correct model so that any get mutators or methods are useable and you can display a previous version in your UI., (*14)
$post->getVersion({version number});
Get current version number., (*15)
$post->getCurrentVersionNumber();
Get previous version for display in UI., (*16)
$post->getPreviousVersion();
Restore to a previous version. All versions after the given version number will be conserved, and the current version will also be saved so that you can later undo., (*17)
$post->restoreVersion({version number});
Similar to restore version, this deletes all the history after the given version so really is like going back in time., (*18)
$post->rollbackToVersion({version number});
Rollback to the last version. This is effectively an 'undo' function to remove the latest version., (*19)
$post->rollback();
Delete a given version., (*20)
$post->deleteVersion({version number});
Delete all version history of a model., (*21)
$post->deleteAllVersions();
Upcoming Features
I'd like to integrate a javascript diff engine to this package so that users can immediately see what has changed between versions., (*22)
I'll be adding some tests soon., (*23)
Contributing
Pull requests welcome for bug fixes, and new useful features., (*24)