PHP code example of makidizajnerica / laravel-snapshoter
1. Go to this page and download the library: Download makidizajnerica/laravel-snapshoter 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/ */
makidizajnerica / laravel-snapshoter example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Snapshoter\HasSnapshots;
use MakiDizajnerica\Snapshoter\Contracts\Snapshotable as SnapshotableContract;
class Project extends Model implements SnapshotableContract
{
use HasSnapshots;
/**
* Get attributes for the snapshot.
*
* @return array<string, mixed>
*/
public function snapshotAttributes(): array
{
return $this->only([
'name',
'description',
'start_at',
'end_at',
]);
}
// ...
}
use App\Models\Project;
use MakiDizajnerica\Snapshoter\Models\Snapshot;
// Make snapshot for the model
$project = Project::first();
$project->makeSnapshot();
// or
snapshoter($project);
// Revert model's state to previous snapshot
$project->revertToPreviousSnapshot();
// Revert model's state to the snapshot from a few steps back
$project->revertToPreviousSnapshot(3);
// You can also pass Snapshot instance
$snapshot = $project->snapshots()->first();
$project->revertToSnapshot($snapshot);
use App\Models\Project;
// Create model with initial snapshot
$project = Project::createWithSnapshot([/* ... */]);
// Update model and make snapshot
$project->updateWithSnapshot([/* ... */]);
// Save model and make snapshot
$project->fill([/* ... */])->saveWithSnapshot();
namespace App\Observers;
use App\Models\Project;
class ProjectObserver
{
/**
* Handle the Project "created" event.
*
* @param \App\Models\Project $project
* @return void
*/
public function created(Project $project)
{
$project->makeSnapshot();
}
/**
* Handle the Project "updated" event.
*
* @param \App\Models\Project $project
* @return void
*/
public function updated(Project $project)
{
$project->makeSnapshot();
}
}