Laraversion is a Laravel package that simplifies version management for your Eloquent models. It allows you to easily track and restore previous versions of your data using a Git-inspired versioning system.
- Features
- Requirements
- Installation
- Usage
- Installing the Graphical User Interface (Optional)
- Commands
- Configuration
- Use Cases
- Contribution
- License
- Automatic version tracking for Eloquent models.
- Easy restoration of previous versions.
- Support for Laravel model events (created, updated, deleted, restored, forceDeleted).
- Storage of version data in a dedicated table with unique UUIDs.
- Easy configuration of the maximum number of versions to retain.
- Events for version creation, pruning, and restoration.
- Ability to specify which model attributes to exclude from versioning.
- PHP ^8.1
- Laravel ^9.0
- Install the package via Composer:
composer require laraversion/laraversion
- Publish the package configuration & migration file:
php artisan vendor:publish --provider="Laraversion\Laraversion\LaraversionServiceProvider"
- Run the migrations:
php artisan migrate
To use Laraversion in your models, add the Laraversion\Laraversion\Traits\Versionable
trait:
use Laraversion\Laraversion\Traits\Versionable;
class YourModel extends Model
{
use Versionable;
}
When using the Versionable trait in your model, the following methods are available:
versionHistory()
: Get the version history for a given model.recordVersion(VersionEventType $eventType)
: Record a new version for the model.revertToVersion(string $commitId)
: Revert the model to a specific version.resetToLastVersion()
: Revert the model to its last modified version.resetToVersionAtDate(Carbon $date)
: Revert the model to the version at a specific date.getVersionDiff(string $commitId1, string $commitId2)
: Get the differences between two versions of a model.
Laraversion fires events when specific actions occur, allowing you to perform additional processing or custom actions. To listen to these events, you can create event listeners and register them in your application.
Here are the events fired by Laraversion:
VersionCreatedEvent
: Triggered when a new version is created for a model.VersionPrunedEvent
: Triggered when an old version is deleted to maintain the maximum number of versions.VersionRestoredEvent
: Triggered when a soft-deleted model is restored.
To create an event listener, you can create a file like the following:
<?php
namespace App\Listeners;
use Laraversion\Laraversion\Events\VersionCreatedEvent;
class VersionCreated
{
public function handle(VersionCreatedEvent $event)
{
// Access the VersionHistory model using $event->version
}
}
Then, you must register the events you want to act on in your App\Providers\EventServiceProvider
's $listen
array:
protected $listen = [
'Laraversion\Laraversion\Events\VersionCreatedEvent' => [
'App\Listeners\VersionCreated',
],
// ...
You can use the Laraversion facade to interact with your models' versions by importing the facade:
use Laraversion\Laraversion\Facades\Laraversion;
Then, you can use the facade's methods:
Laraversion::getVersionHistory(Model $model)
: Get the version history for a given model instance.Laraversion::restoreVersion(Model $model, string $commitId)
: Restore a previous version of a given model instance.Laraversion::restoreToLastVersion(Model $model)
: Revert the given model to its last modified version before current one.Laraversion::getLatestVersion(Model $model)
: Get the latest version of a given model instance.Laraversion::getAllVersions()
: Get all versions of all models.Laraversion::getVersion(Model $model, string $commitId)
: Get a specific version of a given model instance.Laraversion::getVersionDiff(Model $model, string $commitId1, string $commitId2)
: Get the differences between two versions of a given model instance.
use App\Models\User;
use Laraversion\Laraversion\Facades\Laraversion;
$user = User::first();
$versionHistory = Laraversion::getVersionHistory($user);
use App\Models\User;
$user = User::first();
$versionHistory = $user->versionHistory()->get();
// or
$versionHistory = $user->versionHistory;
use App\Models\Post;
use Laraversion\Laraversion\Facades\Laraversion;
$post = Post::firstWhere('slug', 'my-awesome-blog-post');
Laraversion::restoreVersion($post, '123e4567-e89b-12d3-a456-426614174000');
use App\Models\Post;
$post = Post::firstWhere('slug', 'my-awesome-blog-post');
$post->revertToVersion('123e4567-e89b-12d3-a456-426614174000');
Laraversion offers an optional graphical user interface to manage the versions of your models. To install the graphical user interface, run the following command:
php artisan laraversion:install-gui
Once the installation is complete, you can access the Laraversion graphical user interface by visiting the /laraversion
route in your application. The graphical user interface provides the following features:
- List all models being versioned by Laraversion.
- View the version history of a specific model.
- Compare two versions of a model.
- Restore a previous version of a model.
![]() |
![]() |
![]() |
![]() |
Note: Installing the graphical user interface is optional. You can continue using Laraversion without the graphical user interface if you prefer.
Laraversion provides Artisan commands to manage the versions of your models.
php artisan laraversion list
php artisan laraversion:restore {model} {commit_id}
php artisan laraversion:compare {model} {commit_id1} {commit_id2}
Replace {model}
with the model class name and {commit_id1}
and {commit_id2}
with the UUIDs of the versions you want to compare. This command will display a table showing the differences between the two versions.
Example:
php artisan laraversion:compare Post 123e4567-e89b-12d3-a456-426614174000 7890abcd-efgh-3456-ijkl-mnopqrstuvwx
This command will compare the versions of the Post
model with the UUIDs 123e4567-e89b-12d3-a456-426614174000
and 7890abcd-efgh-3456-ijkl-mnopqrstuvwx
, and display a table showing the differences between the two versions.
Note: Make sure to replace Post
, 123e4567-e89b-12d3-a456-426614174000
, and 7890abcd-efgh-3456-ijkl-mnopqrstuvwx
with the actual model class name and version UUIDs you want to compare.
You can find the UUIDs of the available versions using the php artisan laraversion
command.
You can configure the maximum number of versions to retain and the events to listen for versioning in the config/laraversion.php
configuration file.
Here are the keys you can find in the configuration file:
max_versions
: The maximum number of versions to keep for each model.listen_events
: The events to listen for versioning on all models.models
: The models to version and their specific configuration.
For example, you can set the maximum number of versions to retain for a specific model like this:
'models' => [
'App\Models\YourModel' => [
'max_versions' => 5,
'listen_events' => [
'created',
'updated',
// more events here ...
],
],
],
To exclude specific attributes from versioning, define a $untrackedFields
property in your model class:
use Laraversion\Laraversion\Traits\Versionable;
class YourModel extends Model
{
use Versionable;
/**
* The attributes that should not be tracked by the versioning system.
*
* @var array
*/
protected $untrackedFields = ['remember_token', 'email_verified_at'];
}
In this example, the remember_token
and email_verified_at
attributes will not be tracked by Laraversion.
- Audit actions: Use Laraversion to track changes made in your application, allowing you to maintain a comprehensive audit log for compliance and security purposes.
- Collaborative content editing: Use Laraversion to manage content revisions in a collaborative environment, providing a seamless way to track and revert changes made by multiple authors.
- Rollback faulty updates: Use Laraversion to quickly revert to a stable version of your data if an update causes unexpected issues, minimizing downtime and ensuring data integrity.
Contributions are welcome!
Laraversion is open source and released under the MIT license.