diff --git a/Eloquent/Factories/Factory.php b/Eloquent/Factories/Factory.php
index <HASH>..<HASH> 100644
--- a/Eloquent/Factories/Factory.php
+++ b/Eloquent/Factories/Factory.php
@@ -448,22 +448,22 @@ abstract class Factory
     protected function expandAttributes(array $definition)
     {
         return collect($definition)
-            ->map(function ($attribute, $key) {
+            ->map($evaluateRelations = function ($attribute) {
                 if ($attribute instanceof self) {
                     $attribute = $attribute->create()->getKey();
                 } elseif ($attribute instanceof Model) {
                     $attribute = $attribute->getKey();
                 }
 
-                $definition[$key] = $attribute;
-
                 return $attribute;
             })
-            ->map(function ($attribute, $key) use (&$definition) {
+            ->map(function ($attribute, $key) use (&$definition, $evaluateRelations) {
                 if (is_callable($attribute) && ! is_string($attribute) && ! is_array($attribute)) {
                     $attribute = $attribute($definition);
                 }
 
+                $attribute = $evaluateRelations($attribute);
+
                 $definition[$key] = $attribute;
 
                 return $attribute;
[9.x] Factory fails to eval models and factories when returned from closure (#<I>)

* Add test to check a factory closure attribute returning a factory is resolved



* Fix evaluation of factories and models when returned from a closure in factory definition