diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index d9a453359d23..5eb2533425f1 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -201,13 +201,15 @@ public function configure() */ public function raw($attributes = [], ?Model $parent = null) { - if ($this->count === null) { + $count = $this->getCount(); + + if ($count === null) { return $this->state($attributes)->getExpandedAttributes($parent); } return array_map(function () use ($attributes, $parent) { return $this->state($attributes)->getExpandedAttributes($parent); - }, range(1, $this->count)); + }, range(1, $count)); } /** @@ -241,7 +243,7 @@ public function createOneQuietly($attributes = []) public function createMany(int|iterable|null $records = null) { if (is_null($records)) { - $records = $this->count ?? 1; + $records = $this->getCount() ?? 1; } if (is_numeric($records)) { @@ -382,23 +384,25 @@ public function makeOne($attributes = []) */ public function make($attributes = [], ?Model $parent = null) { + $count = $this->getCount(); + if (! empty($attributes)) { return $this->state($attributes)->make([], $parent); } - if ($this->count === null) { + if ($count === null) { return tap($this->makeInstance($parent), function ($instance) { $this->callAfterMaking(collect([$instance])); }); } - if ($this->count < 1) { + if ($count < 1) { return $this->newModel()->newCollection(); } $instances = $this->newModel()->newCollection(array_map(function () use ($parent) { return $this->makeInstance($parent); - }, range(1, $this->count))); + }, range(1, $count))); $this->callAfterMaking($instances); @@ -718,14 +722,30 @@ protected function callAfterCreating(Collection $instances, ?Model $parent = nul /** * Specify how many models should be generated. * - * @param int|null $count + * @param int|callable|null $count * @return static */ - public function count(?int $count) + public function count(int|callable|null $count) { return $this->newInstance(['count' => $count]); } + /** + * Get the count variable and maybe convert it to int. + * + * @return int|null + */ + protected function getCount() + { + $count = $this->count; + + if (is_callable($count)) { + return $count(); + } + + return $count; + } + /** * Specify the database connection that should be used to generate models. *