Skip to content

Commit 42fb6fc

Browse files
committed
performance updates
1 parent f4c2af5 commit 42fb6fc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Attribute/MutableCollection.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ public function replace($value, Model &$object, ?Path $path = null)
4545
$values = collect($value)->pluck('value')->all();
4646

4747
// Check if objects exist
48-
$existingObjects = $object
49-
->{$this->attribute}()
50-
->getRelated()
51-
::findMany($values)
48+
$users = $object
49+
->{$this->attribute}()
50+
->getRelated()
51+
::findMany($values);
52+
$existingObjects = $users
5253
->map(fn ($o) => $o->getKey());
5354

5455
if (($diff = collect($values)->diff($existingObjects))->count() > 0) {
@@ -58,9 +59,9 @@ public function replace($value, Model &$object, ?Path $path = null)
5859
);
5960
}
6061

61-
$object->saved(function (Model $model) use ($existingObjects) {
62+
$object->saved(function (Model $model) use ($existingObjects, $users) {
6263
$model->{$this->attribute}()->sync($existingObjects->all());
63-
$model->load($this->attribute);
64+
$model->setRelation($this->attribute, $users);
6465
});
6566

6667
}

0 commit comments

Comments
 (0)