Skip to content

Commit eb9826d

Browse files
committed
Allow factory attributes to be factory instances themselves
1 parent f854f72 commit eb9826d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Illuminate/Database/Eloquent/FactoryBuilder.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected function getRawAttributes(array $attributes = [])
204204
$this->faker, $attributes
205205
);
206206

207-
return $this->callClosureAttributes(
207+
return $this->expandAttributes(
208208
array_merge($this->applyStates($definition, $attributes), $attributes)
209209
);
210210
}
@@ -254,19 +254,25 @@ protected function applyStates(array $definition, array $attributes = [])
254254
}
255255

256256
/**
257-
* Evaluate any Closure attributes on the attribute array.
257+
* Expand all attributes to their underlying values.
258258
*
259259
* @param array $attributes
260260
* @return array
261261
*/
262-
protected function callClosureAttributes(array $attributes)
262+
protected function expandAttributes(array $attributes)
263263
{
264264
foreach ($attributes as &$attribute) {
265-
$attribute = $attribute instanceof Closure
266-
? $attribute($attributes) : $attribute;
265+
if ($attribute instanceof Closure) {
266+
$attribute = $attribute($attributes);
267+
}
268+
269+
if ($attribute instanceof static) {
270+
$attribute = $attribute->create()->getKey();
271+
}
267272

268-
$attribute = $attribute instanceof Model
269-
? $attribute->getKey() : $attribute;
273+
if ($attribute instanceof Model) {
274+
$attribute = $attribute->getKey();
275+
}
270276
}
271277

272278
return $attributes;

0 commit comments

Comments
 (0)