@@ -41,6 +41,10 @@ public function output(array $tree): array
41
41
foreach ($ tree ['controllers ' ] as $ controller ) {
42
42
$ this ->addImport ($ controller , 'Illuminate \\Http \\Request ' );
43
43
44
+ if ($ controller ->fullyQualifiedNamespace () !== 'App \\Http \\Controllers ' ) {
45
+ $ this ->addImport ($ controller , 'App \\Http \\Controllers \\Controller ' );
46
+ }
47
+
44
48
$ path = $ this ->getPath ($ controller );
45
49
46
50
if (!$ this ->files ->exists (dirname ($ path ))) {
@@ -121,10 +125,10 @@ private function buildMethods(Controller $controller)
121
125
$ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
122
126
} elseif ($ statement instanceof EloquentStatement) {
123
127
$ body .= self ::INDENT . $ statement ->output ($ controller ->prefix (), $ name ) . PHP_EOL ;
124
- $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . '\\' . ( $ controller -> namespace () ? $ controller -> namespace () . '\\' : '' ) . $ this ->determineModel ($ controller , $ statement ->reference ()));
128
+ $ this ->addImport ($ controller , $ this ->determineModel ($ controller , $ statement ->reference ()));
125
129
} elseif ($ statement instanceof QueryStatement) {
126
130
$ body .= self ::INDENT . $ statement ->output ($ controller ->prefix ()) . PHP_EOL ;
127
- $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . '\\' . ( $ controller -> namespace () ? $ controller -> namespace () . '\\' : '' ) . $ this ->determineModel ($ controller , $ statement ->model ()));
131
+ $ this ->addImport ($ controller , $ this ->determineModel ($ controller , $ statement ->model ()));
128
132
}
129
133
130
134
$ body .= PHP_EOL ;
@@ -165,13 +169,21 @@ private function buildImports(Controller $controller)
165
169
private function determineModel (Controller $ controller , ?string $ reference )
166
170
{
167
171
if (empty ($ reference ) || $ reference === 'id ' ) {
168
- return Str::studly (Str::singular ($ controller ->prefix ()));
172
+ return $ this -> fullyQualifyModelReference ( $ controller -> namespace (), Str::studly (Str::singular ($ controller ->prefix () )));
169
173
}
170
174
171
175
if (Str::contains ($ reference , '. ' )) {
172
- return Str::studly (Str::before ($ reference , '. ' ));
176
+ return $ this -> fullyQualifyModelReference ( $ controller -> namespace (), Str::studly (Str::before ($ reference , '. ' ) ));
173
177
}
174
178
175
- return Str::studly ($ reference );
179
+ return $ this ->fullyQualifyModelReference ($ controller ->namespace (), Str::studly ($ reference ));
180
+ }
181
+
182
+ private function fullyQualifyModelReference (string $ sub_namespace , string $ model_name )
183
+ {
184
+ // TODO: get model_name from tree.
185
+ // If not found, assume parallel namespace as controller.
186
+ // Use respond-statement.php as test case.
187
+ return config ('blueprint.namespace ' ) . '\\' . ($ sub_namespace ? $ sub_namespace . '\\' : '' ) . $ model_name ;
176
188
}
177
189
}
0 commit comments