PHP code example of overtrue / laravel-versionable

1. Go to this page and download the library: Download overtrue/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/ */

    

overtrue / laravel-versionable example snippets


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'];

    <...>
}

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

$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(\Carbon\Carbon::create(2022, 10, 6, 12));

$post->getVersion(3)->revert();

// or

$post->revertToVersion(3);

$version = $post->versions()->first();

$post = $version->revertWithoutSaving();

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

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

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

$diff = $post->getVersion(1)->diff($post->getVersion(2));

[
    $attribute1 => $diffOfAttribute1,
    $attribute2 => $diffOfAttribute2,
    ...
    $attributeN => $diffOfAttributeN,
]

$diff->toArray();
//
[
    "name" => [
        "old" => "John",
        "new" => "Doe",
    ],
    "age" => [
        "old" => 25,
        "new" => 26,
    ],
]

toArray(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



class PostVersion extends \Overtrue\LaravelVersionable\Version
{
    //
}


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelVersionable\Versionable;

class Post extends Model
{
    use Versionable;

    public string $versionModel = PostVersion::class;
}
bash
php artisan vendor:publish --provider="Overtrue\LaravelVersionable\ServiceProvider" 
bash
php artisan migrate