Skip to content

Commit c7186e0

Browse files
committed
fix bugs with factory creation
1 parent 13751a1 commit c7186e0

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ protected function buildClass($name)
6666

6767
$model = class_basename($namespaceModel);
6868

69+
if (Str::startsWith($namespaceModel, 'App\\Models')) {
70+
$namespace = Str::beforeLast('Database\\Factories\\'.Str::after($namespaceModel, 'App\\Models\\'), '\\');
71+
} else {
72+
$namespace = 'Database\\Factories';
73+
}
74+
6975
$replace = [
76+
'{{ factoryNamespace }}' => $namespace,
7077
'NamespacedDummyModel' => $namespaceModel,
7178
'{{ namespacedModel }}' => $namespaceModel,
7279
'{{namespacedModel}}' => $namespaceModel,
@@ -88,13 +95,11 @@ protected function buildClass($name)
8895
*/
8996
protected function getPath($name)
9097
{
91-
$name = str_replace(
92-
['\\', '/'], '', $this->argument('name')
93-
);
98+
$name = Str::replaceFirst('App\\', '', $name);
9499

95-
$name = Str::finish($name, 'Factory');
100+
$name = Str::finish($this->argument('name'), 'Factory');
96101

97-
return $this->laravel->databasePath()."/factories/{$name}.php";
102+
return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
98103
}
99104

100105
/**

src/Illuminate/Database/Console/Factories/stubs/factory.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Database\Factories;
3+
namespace {{ factoryNamespace }};
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Illuminate\Support\Str;

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function handle()
7272
*/
7373
protected function createFactory()
7474
{
75-
$factory = Str::studly(class_basename($this->argument('name')));
75+
$factory = Str::studly($this->argument('name'));
7676

7777
$this->call('make:factory', [
7878
'name' => "{$factory}Factory",

0 commit comments

Comments
 (0)