Skip to content

Commit 98375d1

Browse files
onlimetaylorotwell
andauthored
[12.x] Add createMany mass-assignment variants to HasOneOrMany relation (#55262)
* Add createMany mass-assignment variants on HasOneOrMany relation * Fixed docblock for forceCreateManyQuietly() * Update HasOneOrMany.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent e229445 commit 98375d1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

+28
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,34 @@ public function createManyQuietly(iterable $records)
436436
return Model::withoutEvents(fn () => $this->createMany($records));
437437
}
438438

439+
/**
440+
* Create a Collection of new instances of the related model, allowing mass-assignment.
441+
*
442+
* @param iterable $records
443+
* @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
444+
*/
445+
public function forceCreateMany(iterable $records)
446+
{
447+
$instances = $this->related->newCollection();
448+
449+
foreach ($records as $record) {
450+
$instances->push($this->forceCreate($record));
451+
}
452+
453+
return $instances;
454+
}
455+
456+
/**
457+
* Create a Collection of new instances of the related model, allowing mass-assignment and without raising any events to the parent model.
458+
*
459+
* @param iterable $records
460+
* @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
461+
*/
462+
public function forceCreateManyQuietly(iterable $records)
463+
{
464+
return Model::withoutEvents(fn () => $this->forceCreateMany($records));
465+
}
466+
439467
/**
440468
* Set the foreign ID for creating a related model.
441469
*

0 commit comments

Comments
 (0)