1. Go to this page and download the library: Download oobook/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/ */
oobook / snapshot example snippets
namespace App\Models;
use Oobook\Snapshot\Concerns\HasSnapshot;
class MyProduct extends Model
{
use HasSnapshot;
/**
* The source model for the snapshot.
*
* Required - Specifies which model to snapshot from
*
* @var string
*/
public static $snapshotSourceModel = YourModel::class;
/**
* The configuration for the snapshot behavior.
*
* @var array
*/
public static $snapshotConfig = [
// Attributes to take a point-in-time copy of
'snapshot_attributes' => [
'email'
],
// Attributes to keep in sync with the source model
'synced_attributes' => [
'name',
'user_type_id',
],
// Relationships to take a point-in-time copy of
'snapshot_relationships' => [
'posts'
],
// Relationships to keep in sync with the source model
'synced_relationships' => [
'userType',
'fileNames'
],
];
}
// Get the snapshot data
$model->snapshot;
// Access the original source model
$model->source;
// Alternative way to access source model
$model->snapshotSource;
// Create a new snapshot
$snapshot = MyProduct::create([
'your_model_id' => $sourceModel->id,
'name' => 'Custom Name',
'posts' => [1, 2, 3] // IDs of posts to snapshot
]);
// Access snapshotted data
echo $snapshot->email; // Shows snapshotted email
echo $snapshot->name; // Shows synced name from source
// Access relationships
$snapshot->posts; // Shows snapshotted posts
$snapshot->userType; // Shows synced userType from source
// Update source model
$sourceModel->update(['name' => 'New Name']);
echo $snapshot->name; // Shows 'New Name' (synced attribute)
echo $snapshot->email; // Still shows original email (snapshotted attribute)