Easy and conventient way to handle revisions of your models within the database.
- Handles the revisions in bulk - one entry covers all the created/updated fields, what makes it really easy to e.g., compare 2 given versions or get all the data changed during one single transaction.
- This package requires PHP 5.4+
- Currently it works out of the box with Laravel5.4 + generic Illuminate Guard, tymon/jwt-auth OR cartalyst/sentry 2/sentinel 2
composer require johannesschobel/laravel-revisionable
'providers' => array(
...
'JohannesSchobel\Revisionable\RevisionableServiceProvider',
),
~$ php artisan vendor:publish [--provider="JohannesSchobel\Revisionable\RevisionableServiceProvider"]
this will create config/revisionable.php
file, where you can adjust a few settings.
~$ php artisan migrate
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use JohannesSchobel\Revisionable\Traits\Revisionable;
class User extends Model
{
use Revisionable;
}
And that's all to get you started!
The package offers a set of configuration options.
If you would like to revision only specific fields of the model, you can define them like so:
/*
* Set revisionable whitelist - only changes to any
* of these fields will be tracked during updates.
*/
protected $revisionable = [
'email',
'name'
];
This way, only the fields email
and name
are tracked and store as revision in the database. The default behaviour
is to revision all fields.
If you want to disable revisions for a specific model just add the following variables to your model
protected $revisionEnabled = false;
You can further specify that you only want to clean up old revisions of a model. By doing so, you can further define, how many revisions of one model you would like to keep in your database (default is set to 20). Say, if you add the 21st revision, the first one is deleted.
You may customize this behaviour for each model by changing the variables
protected $revisionLimitCleanup = true; // only works with revisionLimit
protected $revisionLimit = 50; // keep 50 instead of 20 revisions of this model
Of course, you can rollback to an old revision of your model. The Trait
added earlier (e.g., the Revisionable
trait)
already provides handy methods for you - so you don't need to worry about this.
You can either use the $model->rollbackToTimestamp($timestamp)
or $model->rollbackSteps($steps)
functions for this
purpose. Note that there is a configuration flag revisionable.rollback.cleanup
(default false
) that indicates,
whether the revisions rolled back should be deleted (true
) or not (false
).
Both functions return the rolled back model, which is already persisted in the database.
$ php artisan tinker
>>> $ticket = App\Models\Ticket::first();
=> <App\Models\Ticket>
>>> $revision->getDiff();
=> [
"customer_id" => [
"old" => "1",
"new" => "101"
],
"item_id" => [
"old" => "2",
"new" => "1"
],
"responsible_id" => [
"old" => "8",
"new" => "2"
]
]
>>> $revision->old('item_id');
=> "2"
>>> $revision->new('item_id');
=> "1"
>>> $revision->isUpdated('item_id');
=> true
>>> $revision->isUpdated('note');
=> false
>>> $revision->label('item_id');
=> "item_id"
>>> $revision->old;
=> [
"defect" => "foo",
"note" => "bar",
"customer_id" => "1",
"item_id" => "2",
"responsible_id" => "8",
"status_id" => "6"
]
>>> $revision->action;
=> "updated"