### Install Versionable Package Source: https://github.com/mpociot/versionable/blob/master/README.md Provides the Composer command to install the Versionable package. This is the first step in integrating versioning into a Laravel project. ```bash composer require mpociot/versionable ``` -------------------------------- ### Get Diff Between Versions Source: https://github.com/mpociot/versionable/blob/master/README.md Calculates the difference between two versions of a model. The `diff` method can be used with or without an argument. If no argument is provided, it defaults to the current version. The result is an associative array of changed attributes. ```php /** * Create a diff against the current version */ $diff = $page->previousVersion()->diff(); /** * Create a diff against a specific version */ $diff = $page->currentVersion()->diff( $version ); ``` -------------------------------- ### Run Versionable Migrations Source: https://github.com/mpociot/versionable/blob/master/README.md Illustrates how to run the database migrations provided by the Versionable package to create the necessary version history tables. ```bash php artisan migrate --path=vendor/mpociot/versionable/src/migrations ``` -------------------------------- ### Publish Versionable Migrations Source: https://github.com/mpociot/versionable/blob/master/README.md Shows how to publish the Versionable package's migrations to the application's database/migrations directory, allowing for customization before running. ```bash php artisan vendor:publish --provider="Mpociot\Versionable\Providers\ServiceProvider" --tag="migrations" ``` -------------------------------- ### Initialize Versions for All Model Instances Source: https://github.com/mpociot/versionable/blob/master/README.md Shows how to create initial versions for all existing instances of a model by calling the static `initializeVersions()` method. This ensures all historical data is captured. ```php Model::initializeVersions(); ``` -------------------------------- ### Create Initial Version for Existing Data Source: https://github.com/mpociot/versionable/blob/master/README.md Explains how to create the first version for an existing model instance using the `createInitialVersion()` method. This is useful when integrating versioning into an existing application. ```php $model->createInitialVersion(); ``` -------------------------------- ### Enable Versioning on a Model Source: https://github.com/mpociot/versionable/blob/master/README.md Demonstrates how to enable model versioning by using the `VersionableTrait` within a Laravel Eloquent model. This automatically tracks changes. ```php class Content extends Model { use Mpociot\Versionable\VersionableTrait; } ``` -------------------------------- ### Revert Model to Previous Version Source: https://github.com/mpociot/versionable/blob/master/README.md Demonstrates how to revert a model to its previous state using the `previousVersion()` and `revert()` methods. This is useful for undoing changes. ```php $content->previousVersion()->revert(); ``` -------------------------------- ### Retrieve All Versions of a Model Source: https://github.com/mpociot/versionable/blob/master/README.md Shows how to access all stored versions associated with a model using the `versions` attribute, which is a Laravel `MorphMany` relation. ```php $model->versions; ``` -------------------------------- ### Retrieve Model from a Specific Version Source: https://github.com/mpociot/versionable/blob/master/README.md Shows how to retrieve a model instance from a specific historical version using the `getModel()` method on a `Version` object. This allows accessing historical data. ```php $oldModel = Version::find(100)->getModel(); ``` -------------------------------- ### Limit Stored Versions Source: https://github.com/mpociot/versionable/blob/master/README.md Demonstrates how to limit the maximum number of stored versions for a model by setting the `$keepOldVersions` property. This helps manage database storage. ```php class User { use VersionableTrait; // Keep the last 10 versions. protected $keepOldVersions = 10; } ``` -------------------------------- ### Use Different Version Table Source: https://github.com/mpociot/versionable/blob/master/README.md Allows specifying a custom table for storing version history. This involves creating a new Version model extending the base `Version` class and setting the `$table` property. The model to be versioned then needs to specify this custom version class using the `$versionClass` property. ```php class MyModelVersion extends Version { protected $table = 'mymodel_versions'; // ... } class MyModel extends Eloquent { use VersionableTrait ; protected $versionClass = MyModelVersion::class ; // ... } ``` -------------------------------- ### Revert to Previous Version Source: https://github.com/mpociot/versionable/blob/master/README.md Reverts a model to its previous version. This method is called on the previous version instance of the model. ```php $content->previousVersion()->revert(); ``` -------------------------------- ### Revert to Specific Version ID Source: https://github.com/mpociot/versionable/blob/master/README.md Reverts a model to a specific version identified by its ID. The `revert` method is called on the found version instance. ```php $revertedModel = Version::find( $version_id )->revert(); ``` -------------------------------- ### Include Hidden Fields in Version Data Source: https://github.com/mpociot/versionable/blob/master/README.md Explains how to include fields that are typically hidden in Eloquent models (using `$hidden` or `$visible`) within the version history data by defining the `$versionedHiddenFields` property. ```php class User { use VersionableTrait; // Typically hidden fields protected $hidden = ['email', 'password']; // Save these hidden fields protected $versionedHiddenFields = ['email', 'password']; } ``` -------------------------------- ### Exclude Attributes from Versioning Source: https://github.com/mpociot/versionable/blob/master/README.md Illustrates how to prevent specific attributes from being included in version history by defining the `$dontVersionFields` property in the model. This is useful for fields like `last_login_at`. ```php class User extends Model { use Mpociot\Versionable\VersionableTrait; /** * @var array */ protected $dontVersionFields = [ 'last_login_at' ]; } ``` -------------------------------- ### Disable Versioning for a Request Source: https://github.com/mpociot/versionable/blob/master/README.md Temporarily disables versioning for a model during the current request. Any updates made after calling `disableVersioning` will not create new version entries. ```php $user = User::find(1); $user->disableVersioning(); // This will not create a new version entry. $user->update([ 'some_attribute' => 'changed value' ]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.