From 083584aeb8ac0f239b3f7bc2009182532b53f8c3 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Wed, 2 Apr 2025 23:57:09 +0200 Subject: [PATCH 1/3] Add createMany mass-assignment variants on HasOneOrMany relation --- .../Eloquent/Relations/HasOneOrMany.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 4142572207af..1ce770186941 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -436,6 +436,35 @@ public function createManyQuietly(iterable $records) return Model::withoutEvents(fn () => $this->createMany($records)); } + /** + * Create a Collection of new instances of the related model. Allow mass-assignment. + * + * @param iterable $records + * @return \Illuminate\Database\Eloquent\Collection + */ + public function forceCreateMany(iterable $records) + { + $instances = $this->related->newCollection(); + + foreach ($records as $record) { + $instances->push($this->forceCreate($record)); + } + + return $instances; + } + + /** + * Create a Collection of new instances of the related model with mass-assignment + * and without raising any events to the parent model + * + * @param iterable $records + * @return \Illuminate\Database\Eloquent\Collection + */ + public function forceCreateManyQuietly(iterable $records) + { + return Model::withoutEvents(fn () => $this->forceCreateMany($records)); + } + /** * Set the foreign ID for creating a related model. * From 832bae437799d17ee74927b333201baf40350c58 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Thu, 3 Apr 2025 11:29:24 +0200 Subject: [PATCH 2/3] Fixed docblock for forceCreateManyQuietly() --- src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 1ce770186941..088c6d3e704c 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -455,7 +455,7 @@ public function forceCreateMany(iterable $records) /** * Create a Collection of new instances of the related model with mass-assignment - * and without raising any events to the parent model + * and without raising any events to the parent model. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection From 5d49ae59d7a3f09b22e4a263b8c2aaa45eba6db7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 4 Apr 2025 11:26:37 -0500 Subject: [PATCH 3/3] Update HasOneOrMany.php --- src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 088c6d3e704c..7451491cbaf9 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -437,7 +437,7 @@ public function createManyQuietly(iterable $records) } /** - * Create a Collection of new instances of the related model. Allow mass-assignment. + * Create a Collection of new instances of the related model, allowing mass-assignment. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection @@ -454,8 +454,7 @@ public function forceCreateMany(iterable $records) } /** - * Create a Collection of new instances of the related model with mass-assignment - * and without raising any events to the parent model. + * Create a Collection of new instances of the related model, allowing mass-assignment and without raising any events to the parent model. * * @param iterable $records * @return \Illuminate\Database\Eloquent\Collection