You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
29
+
30
+
### Basic Setup
31
+
28
32
```php
29
33
<?php
30
34
31
35
namespace App\Models;
32
36
33
-
use Oobook\Snapshot\Traits\HasSnapshot;
37
+
use Oobook\Snapshot\Concerns\HasSnapshot;
34
38
35
39
class MyProduct extends Model
36
40
{
@@ -39,31 +43,120 @@ class MyProduct extends Model
39
43
/**
40
44
* The source model for the snapshot.
41
45
*
42
-
* Required
43
-
*
44
-
* @var Model
45
-
*/
46
-
public $snapshotSourceModel = YourModel::class;
47
-
48
-
/**
49
-
* Fillable attributes to be copied from the source model.
50
-
*
51
-
* Optional
46
+
* Required - Specifies which model to snapshot from
52
47
*
53
-
* @var array
48
+
* @var string
54
49
*/
55
-
public $snapshotSourceFillable = [];
50
+
public static $snapshotSourceModel = YourModel::class;
56
51
57
52
/**
58
-
* Relationships to add to snapshot data.
59
-
*
60
-
* Optional
53
+
* The configuration for the snapshot behavior.
61
54
*
62
55
* @var array
63
56
*/
64
-
public $snapshotSourceRelationships = [];
57
+
public static $snapshotConfig = [
58
+
// Attributes to take a point-in-time copy of
59
+
'snapshot_attributes' => [
60
+
'email'
61
+
],
62
+
63
+
// Attributes to keep in sync with the source model
64
+
'synced_attributes' => [
65
+
'name',
66
+
'user_type_id',
67
+
],
68
+
69
+
// Relationships to take a point-in-time copy of
70
+
'snapshot_relationships' => [
71
+
'posts'
72
+
],
73
+
74
+
// Relationships to keep in sync with the source model
75
+
'synced_relationships' => [
76
+
'userType',
77
+
'fileNames'
78
+
],
79
+
];
65
80
}
66
81
```
82
+
83
+
### Configuration Options
84
+
85
+
#### Snapshot vs Sync
86
+
87
+
The trait provides two ways to handle attributes and relationships:
echo $snapshot->email; // Still shows original email (snapshotted attribute)
136
+
```
137
+
138
+
### Key Features
139
+
140
+
-**Automatic Syncing**: Synced attributes and relationships automatically update when the source model changes
141
+
-**Relationship Handling**: Supports both HasOne/HasMany and BelongsTo/BelongsToMany relationships
142
+
-**Flexible Configuration**: Choose which attributes and relationships to snapshot or sync
143
+
-**Data Integrity**: Maintains separate copies of snapshotted data while keeping synced data up-to-date
144
+
145
+
### Important Notes
146
+
147
+
1. The source model must be specified using `$snapshotSourceModel`
148
+
2. Configuration is optional - by default, all attributes and relationships will be snapshotted (relationships if only you use ManageEloquent Trait on source model)
149
+
3. Synced relationships maintain live connections and may impact performance with large datasets
150
+
4. Snapshotted relationships store a copy of the data at creation time
151
+
5.`Oobook\Database\Eloquent\Concerns\ManageEloquent` Trait is offered to be used on all related models to snapshot mode
152
+
153
+
### Best Practices
154
+
155
+
- Use snapshots for historical records or audit trails
156
+
- Use synced attributes for frequently changing data that should stay current
157
+
- Consider performance implications when syncing large relationships
158
+
- Use relationship IDs instead of full objects when creating snapshots for better performance
0 commit comments