Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Guess the model name when using the make:factory command #34373

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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