Skip to content

Commit fb6b885

Browse files
committed
Allow setting a factory's attribute to a factory instance
The factory will then create the model and use its primary key.
1 parent 51cad04 commit fb6b885

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Illuminate/Database/Eloquent/FactoryBuilder.php

+10-8
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,21 @@ 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;
267-
268-
$attribute = $attribute instanceof Model
269-
? $attribute->getKey() : $attribute;
265+
if ($attribute instanceof Closure) {
266+
$attribute = $attribute($attributes);
267+
} elseif ($attribute instanceof static) {
268+
$attribute = $attribute->create()->getKey();
269+
} elseif ($attribute instanceof Model) {
270+
$attribute = $attribute->getKey();
271+
}
270272
}
271273

272274
return $attributes;

0 commit comments

Comments
 (0)