开源软件名称(OpenSource Name):overtrue/laravel-versionable开源软件地址(OpenSource Url):https://github.com/overtrue/laravel-versionable开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):laravel-versionable It's a minimalist way to make your model support version history, and it's very simple to roll back to the specified version. Requirement
Features
Installingcomposer require overtrue/laravel-versionable -vvv Optional, you can publish the config file: php artisan vendor:publish --provider="Overtrue\LaravelVersionable\ServiceProvider" --tag=config And if you want to custom the migration of the versions table, you can publish the migration file to your database path: php artisan vendor:publish --provider="Overtrue\LaravelVersionable\ServiceProvider" --tag=migrations
Then run this command to create a database migration: php artisan migrate UsageAdd use Overtrue\LaravelVersionable\Versionable;
class Post extends Model
{
use Versionable;
/**
* Versionable attributes
*
* @var array
*/
protected $versionable = ['title', 'content'];
// Or use blacklist
//protected $dontVersionable = ['created_at', 'updated_at'];
<...>
} Versions will be created on vensionable model saved. $post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
$post->update(['title' => 'version2']); Get versions$post->versions; // all versions
$post->latestVersion; // latest version
// or
$post->lastVersion;
$post->versions->first(); // first version
// or
$post->firstVersion; ReversionReversion a model instance to the specified version: $post->getVersion(3)->revert();
// or
$post->revertToVersion(3); Reversion without saving$version = $post->versions()->first();
$post = $version->revertWithoutSaving(); Remove versions// 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(); Restore deleted version by id$post->restoreTrashedVersion($id); Temporarily disable versioning// create
Post::withoutVersion(function () use (&$post) {
Post::create(['title' => 'version1', 'content' => 'version1 content']);
});
// update
Post::withoutVersion(function () use ($post) {
$post->update(['title' => 'updated']);
}); Custom Version Store strategyYou can set the following different version policies through property
Show diff between two versions$diff = $post->getVersion(1)->diff($post->getVersion(2));
You can render the diff to many formats, and all formats result will be like follows: [
$attribute1 => $diffOfAttribute1,
$attribute2 => $diffOfAttribute2,
...
$attributeN => $diffOfAttributeN,
] toArray()$diff->toArray();
//
[
"name" => [
"old" => "John",
"new" => "Doe",
],
"age" => [
"old" => 25,
"new" => 26,
],
] Other formatstoArray(array $differOptions = [], array $renderOptions = []): array
toText(array $differOptions = [], array $renderOptions = []): array
toJsonText(array $differOptions = [], array $renderOptions = []): array
toContextText(array $differOptions = [], array $renderOptions = []): array
toHtml(array $differOptions = [], array $renderOptions = []): array
toInlineHtml(array $differOptions = [], array $renderOptions = []): array
toJsonHtml(array $differOptions = [], array $renderOptions = []): array
toSideBySideHtml(array $differOptions = [], array $renderOptions = []): array
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论