visualbuilder/versionable
Laravel model versioning with polymorphic user support. Track and store a history of changes across multiple user types/guards, keep a set number of versions, whitelist/blacklist attributes, record only diffs, and easily revert models to any saved version.
Fork of overtrue/laravel-versionable with polymorphic user support
This fork extends the original package to support polymorphic user relationships, allowing you to track versions created by different user model types (e.g., Admin, Associate, EndUser, etc.) in applications with multiple authentication guards.
It's a minimalist way to make your model support version history, and it's very simple to revert to the specified version.
This fork replaces the single-user foreign key relationship with a polymorphic relationship, enabling:
auth()->user() to automatically detect the authenticated usermorphs('user') instead of unsignedBigInteger('user_id')morphTo() for the user relationship instead of belongsTo()user_model and user_foreign_key config options (no longer needed)getVersionUser() method returns the authenticated user modelRun:-
composer require visualbuilder/versionable
First, publish the config file and migrations:
php artisan vendor:publish --provider="Visualbuilder\Versionable\ServiceProvider"
Then run this command to create a database migration:
php artisan migrate
Add Visualbuilder\Versionable\Versionable trait to the model and set versionable attributes:
use Visualbuilder\Versionable\Versionable;
class Post extends Model
{
use Versionable;
/**
* Versionable attributes
*
* @var array
*/
protected $versionable = ['title', 'content'];
// Or use a blacklist
//protected $dontVersionable = ['created_at', 'updated_at'];
<...>
}
Versions will be created on the versionable model saved.
$post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
$post->update(['title' => 'version2']);
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Visualbuilder\Versionable\Version;
class Admin extends Authenticatable
{
use SoftDeletes;
public function versions()
{
return $this->morphMany(Version::class, 'user');
}
}
Once you attach the trait to a versionable model, versions automatically store the currently authenticated user.
$admin = Admin::find(1);
auth()->login($admin);
$post = Post::create(['title' => 'Draft', 'content' => '...']);
$version = $post->latestVersion;
$version->user; // instance of Admin
$admin->versions()->latest()->get(); // morphMany inverse relationship
You can repeat the same pattern for any additional authenticatable model that should appear as a version author.
$post->versions; // all versions
$post->latestVersion; // latest version
// or
$post->lastVersion;
$post->versions->first(); // first version
// or
$post->firstVersion;
$post->versionAt('2022-10-06 12:00:00'); // get version from a specific time
// or
$post->versionAt(\Carbon\Carbon::create(2022, 10, 6, 12));
Revert a model instance to the specified version:
$post->getVersion(3)->revert();
// or
$post->revertToVersion(3);
$version = $post->versions()->first();
$post = $version->revertWithoutSaving();
// soft delete
$post->removeVersion($versionId = 1);
$post->removeVersions($versionIds = [1, 2, 3]);
$post->removeAllVersions();
// force delete
$post->forceRemoveVersion($versionId = 1);
$post->forceRemoveVersions($versionIds = [1, 2, 3]);
$post->forceRemoveAllVersions();
$post->restoreTrashedVersion($id);
// create
Post::withoutVersion(function () use (&$post) {
Post::create(['title' => 'version1', 'content' => 'version1 content']);
});
// update
Post::withoutVersion(function () use ($post) {
$post->update(['title' => 'updated']);
});
You can set the following different version policies through property protected $versionStrategy:
Visualbuilder\Versionable\VersionStrategy::DIFF - Version content will only contain changed attributes (default
strategy).Visualbuilder\Versionable\VersionStrategy::SNAPSHOT - Version content will contain all versionable attribute
values.$diff = $post->getVersion(1)->diff($post->getVersion(2));
$diff is a object Visualbuilder\Versionable\Diff, it based
on jfcherng/php-diff.
You can render the diff to many formats, and all formats result will be like follows:
[
$attribute1 => $diffOfAttribute1,
$attribute2 => $diffOfAttribute2,
...
$attributeN => $diffOfAttributeN,
]
$diff->toArray();
//
[
"name" => [
"old" => "John",
"new" => "Doe",
],
"age" => [
"old" => 25,
"new" => 26,
],
]
toArray(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toJsonText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toContextText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toInlineHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toJsonHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
toSideBySideHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array
Note
$differOptionsand$renderOptionsare optional, you can set them following the README of jfcherng/php-diff.$stripTagsallows you to remove HTML tags from the Diff, helpful when you don't want to show tags.
You can define $versionModel in a model, that used this trait to change the model(table) for versions
Note
Model MUST extend class
\Visualbuilder\Versionable\Version;
<?php
class PostVersion extends \Visualbuilder\Versionable\Version
{
//
}
Update the model attribute $versionModel:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Visualbuilder\Versionable\Versionable;
class Post extends Model
{
use Versionable;
public string $versionModel = PostVersion::class;
}
This package is a fork of overtrue/laravel-versionable.
All credit for the original implementation goes to @overtrue. This fork only adds polymorphic user support.
MIT
How can I help you explore Laravel packages today?