Skip to content

Commit 9acf059

Browse files
authored
Guess the model name when using the make:factory command (#34373)
1 parent 670fa30 commit 9acf059

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function buildClass($name)
6262
{
6363
$namespaceModel = $this->option('model')
6464
? $this->qualifyModel($this->option('model'))
65-
: $this->qualifyModel('Model');
65+
: $this->qualifyModel($this->guessModelName($name));
6666

6767
$model = class_basename($namespaceModel);
6868

@@ -102,6 +102,31 @@ protected function getPath($name)
102102
return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
103103
}
104104

105+
/**
106+
* Guess the model name from the Factory name or return a default model name.
107+
*
108+
* @param string $name
109+
* @return string
110+
*/
111+
protected function guessModelName($name)
112+
{
113+
if (Str::endsWith($name, 'Factory')) {
114+
$name = substr($name, 0, -7);
115+
}
116+
117+
$modelName = $this->qualifyModel(class_basename($name));
118+
119+
if (class_exists($modelName)) {
120+
return $modelName;
121+
}
122+
123+
if (is_dir(app_path('Models/'))) {
124+
return 'App\Models\Model';
125+
}
126+
127+
return 'App\Model';
128+
}
129+
105130
/**
106131
* Get the console command options.
107132
*

0 commit comments

Comments
 (0)