Package Data | |
---|---|
Maintainer Username: | johannesschobel@googlemail.com |
Maintainer Contact: | johannes.schobel@googlemail.com (Johannes Schobel) |
Package Create Date: | 2017-05-25 |
Package Last Update: | 2017-11-01 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:10:13 |
Package Statistics | |
---|---|
Total Downloads: | 6,251 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 17 |
Total Watchers: | 3 |
Total Forks: | 2 |
Total Open Issues: | 3 |
Easy and conventient way to handle revisions of your models within the database.
composer.json
:composer require johannesschobel/laravel-revisionable
app/config/app.php
: '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"