diff --git a/src/Illuminate/Database/Eloquent/Factories/Factory.php b/src/Illuminate/Database/Eloquent/Factories/Factory.php index 20bb924c0daf..ee0e74440719 100644 --- a/src/Illuminate/Database/Eloquent/Factories/Factory.php +++ b/src/Illuminate/Database/Eloquent/Factories/Factory.php @@ -705,11 +705,13 @@ public function __call($method, $parameters) $relationship = Str::camel(Str::substr($method, 3)); - $relationshipClass = get_class($this->newModel()->{$relationship}()->getRelated()); + $relatedModel = get_class($this->newModel()->{$relationship}()->getRelated()); - $factory = method_exists($relationshipClass, 'newFactory') && $relationshipClass::newFactory() - ? $relationshipClass::newFactory() - : static::factoryForModel($relationshipClass); + if (method_exists($relatedModel, 'newFactory')) { + $factory = $relatedModel::newFactory() ?: static::factoryForModel($relatedModel); + } else { + $factory = static::factoryForModel($relatedModel); + } if (Str::startsWith($method, 'for')) { return $this->for($factory->state($parameters[0] ?? []), $relationship);