@@ -17,6 +17,9 @@ class FactoryGenerator implements Generator
17
17
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
18
18
private $ files ;
19
19
20
+ /** @var Tree */
21
+ private $ tree ;
22
+
20
23
private $ imports = [];
21
24
22
25
public function __construct ($ files )
@@ -26,6 +29,8 @@ public function __construct($files)
26
29
27
30
public function output (Tree $ tree ): array
28
31
{
32
+ $ this ->tree = $ tree ;
33
+
29
34
$ output = [];
30
35
31
36
if (Blueprint::isLaravel8OrHigher ()) {
@@ -127,9 +132,10 @@ protected function buildDefinition(Model $model)
127
132
}
128
133
129
134
$ class = Str::studly (Str::singular ($ table ));
135
+ $ reference = $ this ->fullyQualifyModelReference ($ class ) ?? $ model ;
130
136
131
137
if (Blueprint::isLaravel8OrHigher ()) {
132
- $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace () . '\\' . $ class );
138
+ $ this ->addImport ($ model , $ reference ->fullyQualifiedNamespace () . '\\' . $ class );
133
139
}
134
140
if ($ key === 'id ' ) {
135
141
if (Blueprint::isLaravel8OrHigher ()) {
@@ -138,7 +144,7 @@ protected function buildDefinition(Model $model)
138
144
$ definition .= ', ' . PHP_EOL ;
139
145
} else {
140
146
$ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
141
- $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace () . '\\' . $ class );
147
+ $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ reference ->fullyQualifiedNamespace () . '\\' . $ class );
142
148
$ definition .= ', ' . PHP_EOL ;
143
149
}
144
150
} else {
@@ -157,14 +163,15 @@ protected function buildDefinition(Model $model)
157
163
} elseif ($ column ->dataType () === 'id ' || ($ column ->dataType () === 'uuid ' && Str::endsWith ($ column ->name (), '_id ' ))) {
158
164
$ name = Str::beforeLast ($ column ->name (), '_id ' );
159
165
$ class = Str::studly ($ column ->attributes ()[0 ] ?? $ name );
166
+ $ reference = $ this ->fullyQualifyModelReference ($ class ) ?? $ model ;
160
167
161
168
if (Blueprint::isLaravel8OrHigher ()) {
162
- $ this ->addImport ($ model , $ model ->fullyQualifiedNamespace () . '\\' . $ class );
169
+ $ this ->addImport ($ model , $ reference ->fullyQualifiedNamespace () . '\\' . $ class );
163
170
$ definition .= str_repeat (self ::INDENT , 3 ) . "' {$ column ->name ()}' => " ;
164
171
$ definition .= sprintf ('%s::factory() ' , $ class );
165
172
} else {
166
173
$ definition .= str_repeat (self ::INDENT , 2 ) . "' {$ column ->name ()}' => " ;
167
- $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ model ->fullyQualifiedNamespace () . '\\' . $ class );
174
+ $ definition .= sprintf ('factory(%s::class) ' , '\\' . $ reference ->fullyQualifiedNamespace () . '\\' . $ class );
168
175
}
169
176
$ definition .= ', ' . PHP_EOL ;
170
177
} elseif (in_array ($ column ->dataType (), ['enum ' , 'set ' ]) && !empty ($ column ->attributes ())) {
@@ -286,4 +293,9 @@ private function fillableColumns(array $columns): array
286
293
return !in_array ('nullable ' , $ column ->modifiers ());
287
294
});
288
295
}
296
+
297
+ private function fullyQualifyModelReference (string $ model_name )
298
+ {
299
+ return $ this ->tree ->modelForContext ($ model_name );
300
+ }
289
301
}
0 commit comments