Skip to content

Commit

Permalink
Allow factory attributes to be factory instances themselves (#19055)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber authored and taylorotwell committed May 4, 2017
1 parent f854f72 commit 06761dd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function getRawAttributes(array $attributes = [])
$this->faker, $attributes
);

return $this->callClosureAttributes(
return $this->expandAttributes(
array_merge($this->applyStates($definition, $attributes), $attributes)
);
}
Expand Down Expand Up @@ -254,19 +254,25 @@ protected function applyStates(array $definition, array $attributes = [])
}

/**
* Evaluate any Closure attributes on the attribute array.
* Expand all attributes to their underlying values.
*
* @param array $attributes
* @return array
*/
protected function callClosureAttributes(array $attributes)
protected function expandAttributes(array $attributes)
{
foreach ($attributes as &$attribute) {
$attribute = $attribute instanceof Closure
? $attribute($attributes) : $attribute;
if ($attribute instanceof Closure) {
$attribute = $attribute($attributes);
}

if ($attribute instanceof static) {
$attribute = $attribute->create()->getKey();
}

$attribute = $attribute instanceof Model
? $attribute->getKey() : $attribute;
if ($attribute instanceof Model) {
$attribute = $attribute->getKey();
}
}

return $attributes;
Expand Down

0 comments on commit 06761dd

Please sign in to comment.