diff --git a/src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php b/src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php index aac32d84cf11..88b9b4a0bba4 100644 --- a/src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php +++ b/src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php @@ -62,7 +62,7 @@ protected function buildClass($name) { $namespaceModel = $this->option('model') ? $this->qualifyModel($this->option('model')) - : $this->qualifyModel('Model'); + : $this->qualifyModel($this->guessModelName($name)); $model = class_basename($namespaceModel); @@ -102,6 +102,31 @@ protected function getPath($name) return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php'; } + /** + * Guess the model name from the Factory name or return a default model name. + * + * @param string $name + * @return string + */ + protected function guessModelName($name) + { + if (Str::endsWith($name, 'Factory')) { + $name = substr($name, 0, -7); + } + + $modelName = $this->qualifyModel(class_basename($name)); + + if (class_exists($modelName)) { + return $modelName; + } + + if (is_dir(app_path('Models/'))) { + return 'App\Models\Model'; + } + + return 'App\Model'; + } + /** * Get the console command options. *