@@ -190,51 +190,64 @@ protected function buildRelationships(Model $model)
190
190
191
191
foreach ($ model ->relationships () as $ type => $ references ) {
192
192
foreach ($ references as $ reference ) {
193
+ // if reference starts with \ , we assume developer is using a fully namespaced model
194
+ $ is_model_fqn = Str::startsWith ($ reference , '\\' );
195
+
193
196
$ custom_template = $ template ;
194
197
$ key = null ;
195
198
$ class = null ;
196
199
197
200
$ column_name = $ reference ;
198
- $ method_name = Str::beforeLast ($ reference , '_id ' );
201
+ $ method_name = $ is_model_fqn ? Str:: afterLast ( $ reference , '\\' ) : Str::beforeLast ($ reference , '_id ' );
199
202
200
203
if (Str::contains ($ reference , ': ' )) {
201
204
[$ foreign_reference , $ column_name ] = explode (': ' , $ reference );
202
- $ method_name = Str::beforeLast ($ column_name , '_id ' );
205
+
206
+ $ method_name = $ is_model_fqn ? Str::afterLast ($ foreign_reference , '\\' ) : Str::beforeLast ($ column_name , '_id ' );
203
207
204
208
if (Str::contains ($ foreign_reference , '. ' )) {
205
209
[$ class , $ key ] = explode ('. ' , $ foreign_reference );
206
210
207
211
if ($ key === 'id ' ) {
208
212
$ key = null ;
209
213
} else {
210
- $ method_name = Str::lower ($ class );
214
+ $ method_name = $ is_model_fqn ? Str:: lower (Str:: afterLast ( $ class , '\\' )) : Str::lower ($ class );
211
215
}
212
216
} else {
213
217
$ class = $ foreign_reference ;
214
218
}
215
219
}
216
220
217
- $ class_name = Str::studly ($ class ?? $ method_name );
218
- $ fqcn = $ this ->fullyQualifyModelReference ($ class_name ) ?? $ model ->fullyQualifiedNamespace () . '\\' . $ class_name ;
221
+ // if full model namespace is proviced we will not try to infer it,
222
+ // we use the namespace as developer gives us
223
+ if ($ is_model_fqn ) {
224
+ $ fqcn = $ class ?? $ column_name ;
225
+ $ class_name = Str::afterLast ($ fqcn , '\\' );
226
+ }else {
227
+ $ class_name = Str::studly ($ class ?? $ method_name );
228
+ $ fqcn = $ this ->fullyQualifyModelReference ($ class_name ) ?? $ model ->fullyQualifiedNamespace () . '\\' . $ class_name ;
229
+ }
230
+
231
+ $ fqcn = Str::startsWith ($ fqcn , '\\' ) ? $ fqcn : '\\' .$ fqcn ;
219
232
220
233
if ($ type === 'morphTo ' ) {
221
234
$ relationship = sprintf ('$this->%s() ' , $ type );
222
235
} elseif ($ type === 'morphMany ' || $ type === 'morphOne ' ) {
223
236
$ relation = Str::lower (Str::singular ($ column_name )) . 'able ' ;
224
- $ relationship = sprintf ('$this->%s(%s::class, \'%s \') ' , $ type , '\\' . $ fqcn , $ relation );
237
+ $ relationship = sprintf ('$this->%s(%s::class, \'%s \') ' , $ type , $ fqcn , $ relation );
225
238
} elseif (!is_null ($ key )) {
226
- $ relationship = sprintf ('$this->%s(%s::class, \'%s \', \'%s \') ' , $ type , '\\' . $ fqcn , $ column_name , $ key );
239
+ $ relationship = sprintf ('$this->%s(%s::class, \'%s \', \'%s \') ' , $ type , $ fqcn , $ column_name , $ key );
227
240
} elseif (!is_null ($ class ) && $ type === 'belongsToMany ' ) {
228
- $ relationship = sprintf ('$this->%s(%s::class, \'%s \') ' , $ type , '\\' . $ fqcn , $ column_name );
241
+ $ relationship = sprintf ('$this->%s(%s::class, \'%s \') ' , $ type , $ fqcn , $ column_name );
229
242
$ column_name = $ class ;
230
243
} else {
231
- $ relationship = sprintf ('$this->%s(%s::class) ' , $ type , '\\' . $ fqcn );
244
+ $ relationship = sprintf ('$this->%s(%s::class) ' , $ type , $ fqcn );
232
245
}
233
246
234
247
if ($ type === 'morphTo ' ) {
235
248
$ method_name = Str::lower ($ class_name );
236
249
} elseif (in_array ($ type , ['hasMany ' , 'belongsToMany ' , 'morphMany ' ])) {
237
- $ method_name = Str::plural ($ column_name );
250
+ $ method_name = Str::plural ($ is_model_fqn ? Str:: afterLast ( $ column_name , '\\' ) : $ column_name );
238
251
}
239
252
240
253
if (Blueprint::supportsReturnTypeHits ()) {
0 commit comments