Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allow Factory::count() to consume callables to be able to generate randomness during runtime. #49637

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down