PHP code example of cape-and-bay / versionable

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

    

cape-and-bay / versionable example snippets


use CapeAndBay\Versionable\Versionable;

class Post extends Model
{
    use Versionable;

    # Prevents versioning Post if only the "is_active" column is updated.
    protected array $dont_version = ['is_active'];
}

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

use CapeAndBay\Versionable\Versionable;
use CapeAndBay\Versionable\VersionableInterface;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements VersionableInterface
{
    use Versionable;
    
    public function onNewVersionCreate(Model $model) : bool
    {
        // Create new version if email is not "[email protected]"
        return $model->email !== '[email protected]';
    }
}

$post->versions; // all versions
$post->latestVersion(); // latest version
$post->versions->first(); // first version
bash
php artisan migrate