Create a snapshot on the stage of creating record and always keeps the currently created data.
oobook/snapshot is a Laravel package for create a snapshot on the stage of creating record and always keeps the currently created data..
It currently has 2 GitHub stars and 4.083 downloads on Packagist (latest version v2.2.0).
Install it with composer require oobook/snapshot.
Discover more Laravel packages by oobook
or browse all Laravel packages to compare alternatives.
Last updated

This package will create easily the snapshots of your eloquent models into another eloquent model. Besides, it retains the relationships as attribute on your projected models.
You can install the package via composer:
composer require oobook/snapshot
Create the snapshot migrations file under database/migrations/ folder using artisan
php artisan vendor:publish --tag="snapshot-migrations"
If you would like to to change snapshot table name, create the snapshot config file under config/ folder using artisan
php artisan vendor:publish --tag="snapshot-config"
The HasSnapshot trait allows you to create point-in-time copies (snapshots) of your Eloquent models while maintaining relationships. It provides both snapshot and sync capabilities for attributes and relationships.
<?php
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'
],
];
}
The trait provides two ways to handle attributes and relationships:
Snapshot Mode (snapshot_attributes, snapshot_relationships)
Sync Mode (synced_attributes, synced_relationships)
The snapshot model automatically provides these relationships:
// 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)
$snapshotSourceModelOobook\Database\Eloquent\Concerns\ManageEloquent Trait is offered to be used on all related models to snapshot modecomposer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.