Skip to content

Keep a revision history for your eloquent models without thinking

License

Notifications You must be signed in to change notification settings

convenia/revisionable

Repository files navigation

logo


Easily create a revision history for any Eloquent model

namespace App;

use Convenia\Revisionable\RevisionableTrait;

class Article extends Eloquent {
  
    use RevisionableTrait;
}

And you're good to go!


This project is a fork of https://github.com/VentureCraft/revisionable with some improvements and new features

The v1 readme is also available if you want to use an old 1.x version


Packagist Build Status StyleCI Codacy Badge Codacy Badge Code Climate Packagist

Installation

Via composer (recommended)

composer require convenia/revisionable:^2.0

Next, you must install the service provider:

// config/app.php
'providers' => [
    ...
    Convenia\Revisionable\RevisionableServiceProvider::class,
];

You can publish the migration with:

php artisan vendor:publish --provider="Convenia\Revisionable\RevisionableServiceProvider" --tag="migrations"

After the migration has been published you can create the revisions table by running the migrations:

php artisan migrate

Docs

Implementation

namespace App;

use Convenia\Revisionable\RevisionableTrait;

class Article extends Eloquent {
  
    use RevisionableTrait;
}

If needed, you can disable the revisioning by setting $revisionEnabled to false in your model. This can be handy if you want to temporarily disable revisioning, or if you want to create your own base model that extends revisionable, which all of your models extend, but you want to turn revisionable off for certain models.

namespace App;

use Convenia\Revisionable\RevisionableTrait;

class Article extends Eloquent {
  
    use RevisionableTrait;
    
    protected $revisionEnabled = false;
}

You can also disable revisioning after X many revisions have been made by setting $historyLimit to the number of revisions you want to keep before stopping revisions.

namespace App;

use Convenia\Revisionable\RevisionableTrait;

class Article extends Eloquent {
  
    use RevisionableTrait;
        
    protected $historyLimit = 500; //Stop tracking revisions after 500 changes have been made.
}

In order to maintain a limit on history, but instead of stopping tracking revisions if you want to remove old revisions, you can accommodate that feature by setting $revisionCleanup.

namespace App;

use Convenia\Revisionable\RevisionableTrait;

class Article extends Eloquent {
  
    use RevisionableTrait;
            
    protected $revisionCleanup = true; //Remove old revisions (works only when used with $historyLimit)
    protected $historyLimit = 500; //Maintain a maximum of 500 changes at any point of time, while cleaning up old revisions.
}

Storing soft deletes

By default, if your model supports soft deletes, revisionable will store this and any restores as updates on the model.

You can choose to ignore deletes and restores by adding deleted_at to your $dontKeepRevisionOf array.

To better format the output for deleted_at entries, you can use the isEmpty formatter (see Format output for an example of this.)

Storing creations

By default the creation of a new model is not stored as a revision. Only subsequent changes to a model is stored.

If you want to store the creation as a revision you can override this behavior by setting revisionCreationsEnabled to true by adding the following to your model:

protected $revisionCreationsEnabled = true;

Contributing

Contributions are encouraged and welcome; to keep things organised, all bugs and requests should be opened in the GitHub issues tab for the main project, at convenia/revisionable/issues