PHP code example of zbiller / laravel-revisions

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

    

zbiller / laravel-revisions example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\Revisions\Options\RevisionOptions;
use Neurony\Revisions\Traits\HasRevisions;

class YourModel extends Model
{
    use HasRevisions;

    /**
     * Get the options for revisioning the model.
     *
     * @return RevisionOptions
     */
    public function getRevisionOptions(): RevisionOptions
    {
        return RevisionOptions::instance();
    }
}

$model = YourModel::find($id);

$revisions = $model->revisions;

// model is state 1
$model = YourModel::find($id);

// model is state 2
// a revision containing the model's state 1 is created 
$model->update(...);

$model = YourModel::find($id);

// a new entry is stored inside the 'revisions' database table
// reflecting the current state of that model record
$model->saveAsRevision();

// model is state 1
$model = YourModel::find($id);
$revision = $model->revisions()->latest()->first();

// model is now in state 0
$model->rollbackToRevision($revision);

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->enableRevisionOnCreate();
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->limitRevisionsTo(100);
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->fieldsToRevision('title', 'content');
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->fieldsToNotRevision('title', 'content');
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->withTimestamps();
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->relationsToRevision('comments', 'author');
}

/**
 * Get the options for revisioning the model.
 *
 * @return RevisionOptions
 */
public function getRevisionOptions(): RevisionOptions
{
    return RevisionOptions::instance()
        ->disableRevisioningWhenRollingBack();
}



namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\Revisions\Options\RevisionOptions;
use Neurony\Revisions\Traits\HasRevisions;

class YourModel extends Model
{
    use HasRevisions;

    /**
     * Boot the model.
     *
     * @return RevisionOptions
     */
    public static function boot()
    {
        parent::boot();

        static::revisioning(function ($model) {
            // your logic here
        });

        static::revisioned(function ($model) {
            // your logic here
        });
    }
    
    /**
     * Get the options for revisioning the model.
     *
     * @return RevisionOptions
     */
    public function getRevisionOptions(): RevisionOptions
    {
        return RevisionOptions::instance();
    }
}

php artisan vendor:publish --provider="Neurony\Revisions\ServiceProvider" --tag="config"

php artisan vendor:publish --provider="Neurony\Revisions\ServiceProvider" --tag="migrations"

php artisan migrate