1
1
<?php namespace Marcelgwerder \ApiHandler ;
2
2
3
+ use \Illuminate \Database \Eloquent \Relations \HasManyThrough ;
4
+ use \Illuminate \Database \Eloquent \Relations \HasOne ;
3
5
use \Illuminate \Database \Eloquent \Relations \HasMany ;
4
6
use \Illuminate \Database \Eloquent \Relations \BelongsTo ;
5
7
use \Illuminate \Database \Eloquent \Relations \BelongsToMany ;
@@ -407,47 +409,46 @@ protected function parseWith($withParam)
407
409
$ model = $ relation ->getModel ();
408
410
$ relationType = $ this ->getRelationType ($ relation );
409
411
410
- //Preserve backwards compatibility
411
- if (method_exists ($ relation , 'getQualifiedOtherKeyName ' ) && method_exists ($ relation , 'getQualifiedForeignKey ' ))
412
+ if ($ relationType === 'HasManyThrough ' )
412
413
{
413
- $ primaryKey = $ relation -> getOtherKey ();
414
- $ foreignKey = $ relation -> getQualifiedForeignKey () ;
415
- }
414
+ $ firstKey = $ model -> getKeyName ();
415
+ $ secondKey = null ;
416
+ }
416
417
else
417
418
{
418
419
$ primaryKey = $ model ->getKeyName ();
419
420
$ foreignKey = $ relation ->getForeignKey ();
420
421
}
421
422
422
423
//Switch keys according to the type of relationship
423
- if ($ relationType == ' HasMany ' )
424
+ if ($ relationType === ' HasOne ' || $ relationType === ' HasMany ' || $ relationType === ' BelongsToMany ' )
424
425
{
425
426
$ firstKey = $ primaryKey ;
426
427
$ secondKey = $ foreignKey ;
427
428
}
428
- else if ($ relationType == 'BelongsTo ' )
429
+ else if ($ relationType === 'BelongsTo ' )
429
430
{
430
431
$ firstKey = $ foreignKey ;
431
432
$ secondKey = $ primaryKey ;
432
433
}
433
-
434
+
434
435
//Check if we're on level 1 (e.g. a and not a.b)
435
- if ($ previousHistoryPath == '' )
436
+ if ($ firstKey !== null && $ previousHistoryPath == '' )
436
437
{
437
438
if ($ fieldsCount > 0 && !in_array ($ primaryKey , $ fields ))
438
439
{
439
440
$ fields [] = $ firstKey ;
440
441
}
441
442
}
442
- else
443
+ else if ( $ firstKey !== null )
443
444
{
444
445
if (count ($ withHistory [$ previousHistoryPath ]['fields ' ]) > 0 && !in_array ($ firstKey , $ withHistory [$ previousHistoryPath ]['fields ' ]))
445
446
{
446
447
$ withHistory [$ previousHistoryPath ]['fields ' ][] = $ firstKey ;
447
448
}
448
449
}
449
450
450
- if (count ($ withHistory [$ currentHistoryPath ]['fields ' ]) > 0 && !in_array ($ secondKey , $ withHistory [$ currentHistoryPath ]['fields ' ]))
451
+ if ($ secondKey !== null && count ($ withHistory [$ currentHistoryPath ]['fields ' ]) > 0 && !in_array ($ secondKey , $ withHistory [$ currentHistoryPath ]['fields ' ]))
451
452
{
452
453
$ withHistory [$ currentHistoryPath ]['fields ' ][] = $ secondKey ;
453
454
}
@@ -473,7 +474,7 @@ protected function parseWith($withParam)
473
474
}, $ withHistory [$ withHistoryKey ]['fields ' ]);
474
475
475
476
if (count ($ fields ) > 0 && is_array ($ fields ))
476
- {
477
+ {
477
478
$ query ->select ($ fields );
478
479
}
479
480
@@ -722,6 +723,11 @@ protected function parseConfig($configParam)
722
723
*/
723
724
protected function getRelationType ($ relation )
724
725
{
726
+ if ($ relation instanceof HasOne)
727
+ {
728
+ return 'HasOne ' ;
729
+ }
730
+
725
731
if ($ relation instanceof HasMany)
726
732
{
727
733
return 'HasMany ' ;
@@ -736,6 +742,11 @@ protected function getRelationType($relation)
736
742
{
737
743
return 'BelongsToMany ' ;
738
744
}
745
+
746
+ if ($ relation instanceof HasManyThrough)
747
+ {
748
+ return 'HasManyThrough ' ;
749
+ }
739
750
}
740
751
741
752
/**
0 commit comments