Skip to content

Commit 1d7e000

Browse files
authored
fix: make model command with deep folder path - has incorrect import HasFactory class path (#53142)
1 parent 964ef32 commit 1d7e000

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ protected function buildFactoryReplacements()
252252
$replacements = [];
253253

254254
if ($this->option('factory')) {
255-
$factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory';
255+
$modelPath = str($this->argument('name'))->studly()->replace('/', '\\')->toString();
256+
257+
$factoryNamespace = '\\Database\\Factories\\'.$modelPath.'Factory';
256258

257259
$factoryCode = <<<EOT
258260
/** @use HasFactory<$factoryNamespace> */

tests/Integration/Generators/ModelMakeCommandTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ModelMakeCommandTest extends TestCase
1010
'app/Http/Controllers/FooController.php',
1111
'app/Http/Controllers/BarController.php',
1212
'database/factories/FooFactory.php',
13+
'database/factories/Foo/BarFactory.php',
1314
'database/seeders/FooSeeder.php',
1415
'tests/Feature/Models/FooTest.php',
1516
];
@@ -119,6 +120,30 @@ public function testItCanGenerateModelFileWithFactoryOption()
119120
$this->assertFilenameNotExists('database/seeders/FooSeeder.php');
120121
}
121122

123+
public function testItCanGenerateModelFileWithFactoryOptionForDeepFolder()
124+
{
125+
$this->artisan('make:model', ['name' => 'Foo/Bar', '--factory' => true])
126+
->assertExitCode(0);
127+
128+
$this->assertFileContains([
129+
'namespace App\Models\Foo;',
130+
'use Illuminate\Database\Eloquent\Factories\HasFactory;',
131+
'use Illuminate\Database\Eloquent\Model;',
132+
'class Bar extends Model',
133+
'/** @use HasFactory<\Database\Factories\Foo\BarFactory> */',
134+
'use HasFactory;',
135+
], 'app/Models/Foo/Bar.php');
136+
137+
$this->assertFileNotContains([
138+
'{{ factoryImport }}',
139+
'{{ factory }}',
140+
], 'app/Models/Foo/Bar.php');
141+
142+
$this->assertFilenameNotExists('app/Http/Controllers/Foo/BarController.php');
143+
$this->assertFilenameExists('database/factories/Foo/BarFactory.php');
144+
$this->assertFilenameNotExists('database/seeders/Foo/BarSeeder.php');
145+
}
146+
122147
public function testItCanGenerateModelFileWithMigrationOption()
123148
{
124149
$this->artisan('make:model', ['name' => 'Foo', '--migration' => true])

0 commit comments

Comments
 (0)