PHP code example of calamandrei-lorenzo / laravel-versionable

1. Go to this page and download the library: Download calamandrei-lorenzo/laravel-versionable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

calamandrei-lorenzo / laravel-versionable example snippets


use CalamandreiLorenzo\LaravelVersionable\Versionable;

class Post extends Model
{
    use Versionable;
    
    /**
     * Versionable attributes
     *
     * @var array
     */
    protected $versionable = ['title', 'content'];
    
    <...>
}

$post = Post::create(['title' => 'version1', 'content' => 'version1 content']);
$post->update(['title' => 'version2']);

$post->versions;

$post->lastVersion;

$post->getVersion('uuid-...')->revert();
$post->getVersionByVersionNumber('uuid-...')->revert();

// or

$post->revertToVersion('uuid-...');
$post->revertToVersionNumber(3);

// 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->restoreThrashedVersion($id);

// create
Post::withoutVersion(function () use (&$post) {
    Post::create(['title' => 'version1', 'content' => 'version1 content']);
});

// update
Post::withoutVersion(function () use ($post) {
    $post->update(['title' => 'updated']);
});
bash
$ php artisan migrate