Skip to content

Commit c0bea1e

Browse files
committed
Find Model using id or create one if none exists
1 parent 59521e7 commit c0bea1e

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/Observers/HasManyObserver.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public function updating(Model $model, $attribute, $value)
1818
->each
1919
->delete();
2020

21-
$childModels = $model->{$attribute}()->get()->all();
21+
for ($i = 0; $i < count($value); $i++) {
22+
$childModel = $model->{$attribute}()->find($value[$i]['modelId']);
2223

23-
for ($i = 0; $i < $count; $i++) {
24-
$i < count($childModels)
25-
? $childModels[$i]->update($value[$i])
26-
: $model->{$attribute}()->create($value[$i]);
24+
if (empty($childModel)) {
25+
$model->{$attribute}()->create($value[$i]['fields']);
26+
} else {
27+
$childModel->update($value[$i]['fields']);
28+
}
2729
}
2830
}
2931

0 commit comments

Comments
 (0)