overtrue/laravel-versionable
Add lightweight version history to Laravel Eloquent models. Track only changed attributes, control fields via whitelist/blacklist, keep a set number of versions, browse versions and revert to any saved point in time with simple APIs and migrations.
Installation:
composer require overtrue/laravel-versionable
php artisan vendor:publish --provider="Overtrue\LaravelVersionable\ServiceProvider"
php artisan migrate
Enable Versioning:
Add the Versionable trait to your model and define $versionable attributes:
use Overtrue\LaravelVersionable\Versionable;
class Post extends Model
{
use Versionable;
protected $versionable = ['title', 'content'];
}
Automatically track changes to title and content fields:
$post = Post::create(['title' => 'Initial', 'content' => 'Draft']);
$post->update(['title' => 'Updated Title']); // Creates a new version
Version Retrieval:
// Get all versions
$versions = $post->versions;
// Get latest version
$latest = $post->latestVersion;
// Get version by ID
$version = $post->getVersion(3);
Reversion:
// Revert to version 2
$post->revertToVersion(2);
// Or via version object
$post->getVersion(2)->revert();
Diffing:
$diff = $post->getVersion(1)->diff($post->getVersion(2));
$diff->toHtml(); // Render as HTML
Temporary Disabling:
Post::withoutVersion(function () {
Post::create(['title' => 'No Version']);
});
Custom Version Model:
class PostVersion extends \Overtrue\LaravelVersionable\Version {}
class Post extends Model {
use Versionable;
public string $versionModel = PostVersion::class;
}
Version Strategies:
class Post extends Model {
use Versionable;
protected $versionStrategy = VersionStrategy::SNAPSHOT; // Full snapshot
}
mansoorkhan96/filament-versionable for UI management.$post->versions->load('model').Post::withoutVersion() in tests.Performance:
$post->load('versions')).withoutVersion() for bulk operations to skip versioning.Diff Strategy:
DIFF (default) only stores changed attributes, but may miss context.SNAPSHOT stores full copies, useful for complex data.Soft Deletes:
forceRemoveVersion() to permanently delete versions.restoreTrashedVersion($id).Version Not Created?
$versionable attributes are whitelisted.Diff Issues:
$stripTags = true for HTML content.Custom Model Errors:
\Overtrue\LaravelVersionable\Version.Custom Version Logic:
Override createVersion() or shouldCreateVersion() in your model.
Version Storage:
Extend the Version model to add custom fields (e.g., user_id).
Diff Formatting:
Customize diff output by extending the Diff class or using php-diff options.
id as primary key by default.created_at for version ordering.How can I help you explore Laravel packages today?