mpociot / versionable
Allows to create Laravel 4 / 5 / 6 / 7 Model versioning and restoring
Installs: 119 763
Dependents: 3
Suggesters: 0
Security: 0
Stars: 418
Watchers: 18
Forks: 70
Open Issues: 15
Requires
- php: >=7.1.0 || >=7.2.5
- illuminate/support: ~5.3 || ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.1 || ^4.0 || ^5.0 || ^6.0
- phpunit/phpunit: ^7.0 || ^8.0 || ^9.0
README
Laravel Model versioning made easy
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();
Installation
composer require mpociot/versionable
php artisan migrate --path=vendor/mpociot/versionable/src/migrations
Alternatively, publish the migrations.
php artisan vendor:publish --provider="Mpociot\Versionable\Providers\ServiceProvider" --tag="migrations"
php artisan migrate
Usage
Let the Models you want to set under version control use the VersionableTrait
.
class Content extends Model { use Mpociot\Versionable\VersionableTrait; }
All timestamps and the optional soft-delete timestamp will be ignored.
Exclude attributes from versioning
class User extends Model { use Mpociot\Versionable\VersionableTrait; /** * @var array */ protected $dontVersionFields = [ 'last_login_at' ]; }
Maximum number of stored versions
You can do this by setting a $keepOldVersions
property on your versionable models:
class User { use VersionableTrait; // Keep the last 10 versions. protected $keepOldVersions = 10; }
Retrieving all versions associated to a model
To retrieve all stored versions use the versions
attribute on your model.
$model->versions;
Getting a diff of two versions
/** * Create a diff against the current version */ $diff = $page->previousVersion()->diff(); /** * Create a diff against a specific version */ $diff = $page->currentVersion()->diff( $version );
Revert to a previous 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();
Disable versioning
$user = User::find(1); $user->disableVersioning(); // This will not create a new version entry. $user->update([ 'some_attribute' => 'changed value' ]);
Use different version table
class MyModelVersion extends Version { $table = 'mymodel_versions'; // ... }
class MyModel extends Eloquent { use VersionableTrait ; protected $versionClass = MyModelVersion::class ; // ... }
License
Versionable is free software distributed under the terms of the MIT license.