5
5
use \Illuminate \Database \Eloquent \Relations \HasMany ;
6
6
use \Illuminate \Database \Eloquent \Relations \BelongsTo ;
7
7
use \Illuminate \Database \Eloquent \Relations \BelongsToMany ;
8
+ use \Illuminate \Database \Eloquent \Relations \MorphOne ;
9
+ use \Illuminate \Database \Eloquent \Relations \MorphMany ;
8
10
use \Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
9
11
use \Illuminate \Database \Query \Builder as QueryBuilder ;
10
12
use \Illuminate \Support \Facades \Config ;
@@ -406,36 +408,36 @@ protected function parseWith($withParam)
406
408
}
407
409
408
410
$ relation = call_user_func ([$ previousModel , $ part ]);
409
- $ model = $ relation ->getModel ();
410
411
$ relationType = $ this ->getRelationType ($ relation );
411
412
412
- if ($ relationType === 'HasManyThrough ' )
413
+ if ($ relationType === 'BelongsTo ' )
413
414
{
414
- $ firstKey = $ model ->getKeyName ();
415
- $ secondKey = null ;
416
- }
417
- else
418
- {
419
- $ primaryKey = $ model ->getKeyName ();
420
- $ foreignKey = $ relation ->getForeignKey ();
421
- }
422
-
423
- //Switch keys according to the type of relationship
424
- if ($ relationType === 'HasOne ' || $ relationType === 'HasMany ' || $ relationType === 'BelongsToMany ' )
415
+ $ firstKey = $ relation ->getQualifiedForeignKey ();
416
+ $ secondKey = $ relation ->getQualifiedParentKeyName ();
417
+ }
418
+ else if ($ relationType === 'HasMany ' || $ relationType === 'HasOne ' )
425
419
{
426
- $ firstKey = $ primaryKey ;
427
- $ secondKey = $ foreignKey ;
428
- }
429
- else if ($ relationType === 'BelongsTo ' )
420
+ $ firstKey = $ relation -> getQualifiedParentKeyName () ;
421
+ $ secondKey = $ relation -> getForeignKey () ;
422
+ }
423
+ else if ($ relationType === 'BelongsToMany ' )
430
424
{
431
- $ firstKey = $ foreignKey ;
432
- $ secondKey = $ primaryKey ;
425
+ $ firstKey = $ relation -> getQualifiedParentKeyName () ;
426
+ $ secondKey = $ relation -> getRelated ()-> getQualifiedKeyName () ;
433
427
}
428
+ else if ($ relationType === 'HasManyThrough ' )
429
+ {
430
+ $ firstKey = $ relation ->getHasCompareKey ();
431
+ $ secondKey = null ;
432
+ }
433
+ else {
434
+ die ('Relation type not supported! ' );
435
+ }
434
436
435
437
//Check if we're on level 1 (e.g. a and not a.b)
436
438
if ($ firstKey !== null && $ previousHistoryPath == '' )
437
439
{
438
- if ($ fieldsCount > 0 && !in_array ($ primaryKey , $ fields ))
440
+ if ($ fieldsCount > 0 && !in_array ($ firstKey , $ fields ))
439
441
{
440
442
$ fields [] = $ firstKey ;
441
443
}
@@ -453,7 +455,7 @@ protected function parseWith($withParam)
453
455
$ withHistory [$ currentHistoryPath ]['fields ' ][] = $ secondKey ;
454
456
}
455
457
456
- $ previousModel = $ model ;
458
+ $ previousModel = $ relation -> getModel () ;
457
459
}
458
460
459
461
unset($ previousModel );
@@ -788,6 +790,16 @@ protected function getRelationType($relation)
788
790
{
789
791
return 'HasManyThrough ' ;
790
792
}
793
+
794
+ if ($ relation instanceof MorphOne)
795
+ {
796
+ return 'MorphOne ' ;
797
+ }
798
+
799
+ if ($ relation instanceof MorphMany)
800
+ {
801
+ return 'MorphMany ' ;
802
+ }
791
803
}
792
804
793
805
/**
0 commit comments