8
8
use Froiden \RestAPI \Exceptions \Parse \MaxLimitException ;
9
9
use Froiden \RestAPI \Exceptions \Parse \NotAllowedToFilterOnThisFieldException ;
10
10
use Illuminate \Database \Eloquent \Relations \BelongsTo ;
11
+ use Illuminate \Database \Eloquent \Relations \HasMany ;
11
12
use Illuminate \Database \Eloquent \Relations \HasOne ;
12
13
use Illuminate \Support \Str ;
13
14
@@ -413,12 +414,28 @@ private function parseFields($fields)
413
414
414
415
if (Str::contains ($ fieldName , ". " )) {
415
416
416
- // TODO: add support for non standard relation column names for multi level relations
417
+ $ relationNameParts = explode ('. ' , $ fieldName );
418
+ $ model = $ this ->model ;
419
+
420
+ $ relation = null ;
421
+
422
+ foreach ($ relationNameParts as $ rp ) {
423
+ $ relation = call_user_func ([ new $ model (), $ rp ]);
424
+ $ model = $ relation ->getRelated ();
425
+ }
417
426
418
427
// Its a multi level relations
419
428
$ fieldParts = explode (". " , $ fieldName );
420
429
421
- $ singular = Str::singular (last ($ fieldParts ));
430
+ if ($ relation instanceof BelongsTo) {
431
+ $ singular = $ relation ->getOtherKey ();
432
+ }
433
+ else if ($ relation instanceof HasOne || $ relation instanceof HasMany) {
434
+ $ singular = explode ('. ' , $ relation ->getForeignKey ())[1 ];
435
+ }
436
+ else {
437
+ $ singular = Str::singular (last ($ fieldParts ));
438
+ }
422
439
423
440
// Unset last element of array
424
441
unset($ fieldParts [count ($ fieldParts ) - 1 ]);
@@ -432,12 +449,12 @@ private function parseFields($fields)
432
449
"limit " => config ("api.defaultLimit " ),
433
450
"offset " => 0 ,
434
451
"order " => "chronological " ,
435
- "fields " => [$ singular . " _id " ],
452
+ "fields " => [$ singular ],
436
453
"userSpecifiedFields " => true
437
454
];
438
455
}
439
456
else {
440
- $ this ->relations [$ parent ]["fields " ][] = $ singular . " _id " ;
457
+ $ this ->relations [$ parent ]["fields " ][] = $ singular ;
441
458
}
442
459
}
443
460
else {
0 commit comments