Skip to content

Commit cb5778e

Browse files
committed
Create addIdToParentRelationData method to prevent repeating code;
1 parent f275560 commit cb5778e

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Relations/MorphToMany.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentMorphToMany;
1111
use Illuminate\Support\Arr;
1212

13+
use MongoDB\BSON\ObjectId;
1314
use function array_diff;
1415
use function array_key_exists;
1516
use function array_keys;
@@ -219,9 +220,7 @@ public function attach($id, array $attributes = [], $touch = true)
219220
],
220221
], true);
221222
}else{
222-
$instance = new $this->related();
223-
$instance->forceFill([$this->relatedKey => $id]);
224-
$this->parent->setRelation($this->relationName, $this->parent->{$this->relationName}->push($instance)->unique($this->relatedKey));
223+
$this->addIdToParentRelationData($id);
225224
}
226225

227226
// Attach the new parent id to the related model.
@@ -239,9 +238,7 @@ public function attach($id, array $attributes = [], $touch = true)
239238
if ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
240239
$this->parent->push($this->relatedPivotKey, (array) $id, true);
241240
}else{
242-
$instance = new $this->related();
243-
$instance->forceFill([$this->relatedKey => $id]);
244-
$this->parent->setRelation($this->relationName, $this->parent->{$this->relationName}->push($instance));
241+
$this->addIdToParentRelationData($id);
245242
}
246243
}
247244
} else {
@@ -270,9 +267,7 @@ public function attach($id, array $attributes = [], $touch = true)
270267
}
271268
} else {
272269
foreach ($id as $item) {
273-
$instance = new $this->related();
274-
$instance->forceFill([$this->relatedKey => $item]);
275-
$this->parent->setRelation($this->relationName, $this->parent->{$this->relationName}->push($instance));
270+
$this->addIdToParentRelationData($item);
276271
}
277272
}
278273
} else {
@@ -289,9 +284,7 @@ public function attach($id, array $attributes = [], $touch = true)
289284
$this->parent->push($this->relatedPivotKey, $id, true);
290285
} else {
291286
foreach ($id as $item) {
292-
$instance = new $this->related();
293-
$instance->forceFill([$this->relatedKey => $item]);
294-
$this->parent->setRelation($this->relationName, $this->parent->{$this->relationName}->push($instance));
287+
$this->addIdToParentRelationData($item);
295288
}
296289
}
297290
}
@@ -456,4 +449,21 @@ public function extractIds(array $data, ?string $relatedPivotKey = null)
456449
return $carry;
457450
}, []);
458451
}
452+
453+
454+
/**
455+
* Add the given id to the relation's data of the current parent instance.
456+
* It helps to keep up-to-date the sql model instances in hybrid relationships.
457+
*
458+
* @param ObjectId|string|int $id
459+
*
460+
* @return void
461+
*/
462+
private function addIdToParentRelationData($id)
463+
{
464+
$instance = new $this->related();
465+
$instance->forceFill([$this->relatedKey => $id]);
466+
$relationData = $this->parent->{$this->relationName}->push($instance)->unique($this->relatedKey);
467+
$this->parent->setRelation($this->relationName, $relationData);
468+
}
459469
}

0 commit comments

Comments
 (0)