Skip to content

Commit baa16eb

Browse files
fix(trait): Add null checks in snapshot source field population
1 parent 4df3a2c commit baa16eb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Traits/HasSnapshot.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,19 @@ public static function bootHasSnapshot()
146146
$snapshot = $model->getSnapshot();
147147
$source = $model->source()->with($model->getSnapshotSourceRelationships())->first();
148148

149-
$fields = array_merge($source->toArray(), $snapshot->getAttributes());
149+
if($source && $snapshot){
150+
$fields = array_merge($source->toArray(), $snapshot->getAttributes());
150151

151-
foreach ($fields as $fieldName => $value) {
152-
if (!$model->{$fieldName}) {
153-
$model->_snapshotSourceFields[$fieldName] = $value;
154-
$model->setAttribute($fieldName, $value);
152+
foreach ($fields as $fieldName => $value) {
153+
if (!$model->{$fieldName}) {
154+
$model->_snapshotSourceFields[$fieldName] = $value;
155+
$model->setAttribute($fieldName, $value);
156+
}
155157
}
156-
}
157158

158-
$model[$foreignKey] = $model->snapshot->source_id;
159-
$model->_snapshotSourceFields[$foreignKey] = $model[$foreignKey];
159+
$model[$foreignKey] = $model->snapshot->source_id;
160+
$model->_snapshotSourceFields[$foreignKey] = $model[$foreignKey];
161+
}
160162
}
161163
});
162164
}

0 commit comments

Comments
 (0)