10
10
use Illuminate \Database \Eloquent \Relations \MorphToMany as EloquentMorphToMany ;
11
11
use Illuminate \Support \Arr ;
12
12
13
+ use MongoDB \BSON \ObjectId ;
13
14
use function array_diff ;
14
15
use function array_key_exists ;
15
16
use function array_keys ;
@@ -219,9 +220,7 @@ public function attach($id, array $attributes = [], $touch = true)
219
220
],
220
221
], true );
221
222
}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 );
225
224
}
226
225
227
226
// Attach the new parent id to the related model.
@@ -239,9 +238,7 @@ public function attach($id, array $attributes = [], $touch = true)
239
238
if ($ this ->parent instanceof \MongoDB \Laravel \Eloquent \Model) {
240
239
$ this ->parent ->push ($ this ->relatedPivotKey , (array ) $ id , true );
241
240
}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 );
245
242
}
246
243
}
247
244
} else {
@@ -270,9 +267,7 @@ public function attach($id, array $attributes = [], $touch = true)
270
267
}
271
268
} else {
272
269
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 );
276
271
}
277
272
}
278
273
} else {
@@ -289,9 +284,7 @@ public function attach($id, array $attributes = [], $touch = true)
289
284
$ this ->parent ->push ($ this ->relatedPivotKey , $ id , true );
290
285
} else {
291
286
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 );
295
288
}
296
289
}
297
290
}
@@ -456,4 +449,21 @@ public function extractIds(array $data, ?string $relatedPivotKey = null)
456
449
return $ carry ;
457
450
}, []);
458
451
}
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
+ }
459
469
}
0 commit comments