### Install Laravel Versionable Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Installs the package via Composer and publishes configuration and migrations. ```shell composer require overtrue/laravel-versionable -vvv php artisan vendor:publish --provider="Overtrue\LaravelVersionable\ServiceProvider" php artisan migrate ``` -------------------------------- ### Showing Differences Between Versions Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Illustrates how to get and display the differences between two versions of a model. ```php $diff = $post->getVersion(1)->diff($post->getVersion(2)); // $diff is an object Overtrue\LaravelVersionable\Diff // You can render the diff to many formats $diff->toArray(); // // [ // "name" => [ // "old" => "John", // "new" => "Doe", // ], // "age" => [ // "old" => 25, // "new" => 26, // ], // ] ``` -------------------------------- ### Custom Version Store Strategy Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Details the available version storage strategies: DIFF and SNAPSHOT. ```php You can set the following different version policies through property `protected $versionStrategy`: - `Overtrue\LaravelVersionable\VersionStrategy::DIFF` - Version content will only contain changed attributes (default strategy). - `Overtrue\LaravelVersionable\VersionStrategy::SNAPSHOT` - Version content will contain all versionable attribute values. ``` -------------------------------- ### Creating and Updating Versions Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Shows how versions are automatically created when a versionable model is saved or updated. ```php $post = Post::create(['title' => 'version1', 'content' => 'version1 content']); $post->update(['title' => 'version2']); ``` -------------------------------- ### Retrieving Versions Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Explains how to access all versions, the latest version, the first version, and versions from a specific time. ```php $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(CarbonCarbon::create(2022, 10, 6, 12)); ``` -------------------------------- ### Basic Model Usage Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Demonstrates how to use the Versionable trait in a Laravel model and define versionable attributes. ```php use Overtrue\LaravelVersionable\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']; // ... } ``` -------------------------------- ### Restoring Deleted Versions Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Shows how to restore a version that has been soft-deleted. ```php $post->restoreTrashedVersion($id); ``` -------------------------------- ### Output Formats Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Provides methods to convert version data into different formats such as arrays, plain text, JSON text, and various HTML representations. These methods accept optional differ and render options for customization. ```php 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 oJsonHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array toSideBySideHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array ``` -------------------------------- ### Laravel Versionable API Documentation Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md API documentation for the Laravel Versionable package, covering model traits, methods, and versioning strategies. ```APIDOC Overtrue\LaravelVersionable\Versionable Trait: - Use this trait in your Eloquent models to enable versioning. - Properties: - `$versionable`: An array of attributes to track versions for. - `$dontVersionable`: An array of attributes to exclude from versioning (used as a blacklist). - `$versionStrategy`: Specifies the version storage strategy ('DIFF' or 'SNAPSHOT'). Methods: - `versions()`: Returns a relationship to all versions of the model. - `latestVersion()`: Returns the most recent version of the model. - `firstVersion()`: Returns the oldest version of the model. - `versionAt(string $timestamp)`: Retrieves the version of the model at a specific timestamp. - `getVersion(int $versionId)`: Retrieves a specific version of the model by its ID. - `revert()`: Reverts the model instance to its current version. - `revertToVersion(int $versionId)`: Reverts the model instance to a specified version ID. - `revertWithoutSaving()`: Reverts the model instance without saving changes. - `removeVersion(int $versionId)`: Soft deletes a specific version. - `removeVersions(array $versionIds)`: Soft deletes multiple versions. - `removeAllVersions()`: Soft deletes all versions. - `forceRemoveVersion(int $versionId)`: Permanently deletes a specific version. - `forceRemoveVersions(array $versionIds)`: Permanently deletes multiple versions. - `forceRemoveAllVersions()`: Permanently deletes all versions. - `restoreTrashedVersion(int $id)`: Restores a soft-deleted version. - `diff(Version $version)`: Calculates the difference between the current model version and another version. - `withoutVersion(callable $callback)`: Executes a callback without creating new versions. Version Strategies: - `Overtrue\LaravelVersionable\VersionStrategy::DIFF`: - Stores only the attributes that have changed between versions. - Default strategy. - `Overtrue\LaravelVersionable\VersionStrategy::SNAPSHOT`: - Stores all versionable attributes for each version. ``` -------------------------------- ### Custom Version Model Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Demonstrates how to define and use a custom version model by extending the base `\Overtrue\LaravelVersionable\Version` class and specifying it in the `$versionModel` attribute of your Eloquent model. ```php getVersion(3)->revert(); // or $post->revertToVersion(3); // Revert without saving $version = $post->versions()->first(); $post = $version->revertWithoutSaving(); ``` -------------------------------- ### Removing Versions Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Demonstrates how to remove specific versions, multiple versions, or all versions using soft delete or force delete. ```php // 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(); ``` -------------------------------- ### Temporarily Disabling Versioning Source: https://github.com/overtrue/laravel-versionable/blob/5.x/README.md Explains how to prevent version history from being created during model creation or updates. ```php // create Post::withoutVersion(function () use (&$post) { Post::create(['title' => 'version1', 'content' => 'version1 content']); }); // update Post::withoutVersion(function () use ($post) { $post->update(['title' => 'updated']); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.