Skip to content

Commit

Permalink
Guess the model name when using the make:factory command
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Sep 17, 2020
1 parent bf32c44 commit f1b98bc
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Illuminate/Database/Console/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit f1b98bc

Please sign in to comment.