Skip to content

Commit 5b0d72c

Browse files
authored
[12.x] Resolve issue with BelongsToManyRelationship factory (#55608)
* Update BelongsToManyRelationship.php * cs
1 parent 12befda commit 5b0d72c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Illuminate/Database/Eloquent/Factories/BelongsToManyRelationship.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ public function __construct($factory, $pivot, $relationship)
5050
*/
5151
public function createFor(Model $model)
5252
{
53-
$relationship = $model->{$this->relationship}();
53+
$factoryInstance = $this->factory instanceof Factory;
5454

55-
Collection::wrap($this->factory instanceof Factory ? $this->factory->prependState($relationship->getQuery()->pendingAttributes)->create([], $model) : $this->factory)->each(function ($attachable) use ($model) {
55+
if ($factoryInstance) {
56+
$relationship = $model->{$this->relationship}();
57+
}
58+
59+
Collection::wrap($factoryInstance ? $this->factory->prependState($relationship->getQuery()->pendingAttributes)->create([], $model) : $this->factory)->each(function ($attachable) use ($model) {
5660
$model->{$this->relationship}()->attach(
5761
$attachable,
5862
is_callable($this->pivot) ? call_user_func($this->pivot, $model) : $this->pivot

0 commit comments

Comments
 (0)