1. Go to this page and download the library: Download grazulex/laravel-snapshot 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/ */
grazulex / laravel-snapshot example snippets
use Grazulex\LaravelSnapshot\Traits\HasSnapshots;
class User extends Model
{
use HasSnapshots;
// Optionally specify which attributes to track
protected $snapshotable = ['name', 'email', 'status'];
// Exclude sensitive attributes
protected $snapshotExclude = ['password', 'remember_token'];
}
// Automatic snapshots on model events
class User extends Model
{
use HasSnapshots;
protected $snapshotEvents = ['created', 'updated', 'deleted'];
// Custom snapshot triggers
protected $snapshotTriggers = [
'status_changed' => function ($model) {
return $model->isDirty('status');
}
];
}
$user = User::find(1);
// Create manual snapshot
$snapshot = $user->createSnapshot('Before important update');
// Update the model
$user->update(['status' => 'active', 'email' => '[email protected]']);
// Get all snapshots
$snapshots = $user->snapshots()->orderBy('created_at', 'desc')->get();
// Get specific snapshot
$latestSnapshot = $user->latestSnapshot();
$firstSnapshot = $user->firstSnapshot();
// Restore to latest snapshot
$user->restoreFromLatestSnapshot();
// Restore to specific snapshot
$user->restoreFromSnapshot($snapshot);
// Restore to specific date
$user->restoreToDate(now()->subDays(7));
// Preview restoration (without saving)
$previewData = $user->previewRestore($snapshot);