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:
- Support for multiple user model types (Admin, Associate, OrganisationUser, etc.)
- Automatic tracking of which user type created each version
- Compatibility with applications using multiple authentication guards
- No configuration needed - uses
auth()->user()to automatically detect the authenticated user
- Migration: Uses
morphs('user')instead ofunsignedBigInteger('user_id') - Version Model: Uses
morphTo()for the user relationship instead ofbelongsTo() - Configuration: Removed
user_modelanduser_foreign_keyconfig options (no longer needed) - Versionable Trait: New
getVersionUser()method returns the authenticated user model
- PHP >= 8.1.0
- laravel/framework >= 9.0
- Keep the specified number of versions.
- Whitelist and blacklist for versionable attributes.
- Easily revert to the specified version.
- Record only changed attributes.
- Easy to customize.
Run:-
composer require visualbuilder/versionableFirst, 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 migrateAdd 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 relationshipYou 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): arrayNote
$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;
}- mansoorkhan96/filament-versionable Effortlessly manage revisions of your Eloquent models in Filament.
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