@@ -33,48 +33,52 @@ public static function bootManageEloquent()
3333
3434 static ::$ manageEloquentDefinedRelationships [$ class ] = collect ($ reflector ->getMethods (\ReflectionMethod::IS_PUBLIC ))
3535 ->reduce (function ($ carry , \ReflectionMethod $ method ) use ($ relationClassesPattern , $ self ) {
36- if ($ method ->hasReturnType () && preg_match ("{$ relationClassesPattern }" , ($ returnType = $ method ->getReturnType ()) )){
37- $ relationshipMethodName = $ method ->name ;
38- $ relationship = $ self ->{ $ relationshipMethodName }();
39- $ related = $ relationship ->getRelated ();
40- $ relationshipTable = $ related ->getTable ();
41-
42- // if many to many, get the pivot or through table
43- $ hasMiddlemanModel = false ;
44- $ middlemanModel = null ;
45- $ middlemanTable = null ;
46- $ isManyToMany = false ;
47-
48- if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \MorphToMany){
49- $ isManyToMany = true ;
50- $ middlemanTable = $ relationship ->getTable ();
51- }else if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \BelongsToMany){
52- $ isManyToMany = true ;
53- $ pivotClass = $ relationship ->getPivotClass ();
54-
55- if (!($ pivotClass instanceof \Illuminate \Database \Eloquent \Relations \Pivot)){
36+ try {
37+ if ($ method ->hasReturnType () && preg_match ("{$ relationClassesPattern }" , ($ returnType = $ method ->getReturnType ()) )){
38+ $ relationshipMethodName = $ method ->name ;
39+ $ relationship = $ self ->{ $ relationshipMethodName }();
40+ $ related = $ relationship ->getRelated ();
41+ $ relationshipTable = $ related ->getTable ();
42+
43+ // if many to many, get the pivot or through table
44+ $ hasMiddlemanModel = false ;
45+ $ middlemanModel = null ;
46+ $ middlemanTable = null ;
47+ $ isManyToMany = false ;
48+
49+ if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \MorphToMany){
50+ $ isManyToMany = true ;
51+ $ middlemanTable = $ relationship ->getTable ();
52+ }else if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \BelongsToMany){
53+ $ isManyToMany = true ;
54+ $ pivotClass = $ relationship ->getPivotClass ();
55+
56+ if (!($ pivotClass instanceof \Illuminate \Database \Eloquent \Relations \Pivot)){
57+ $ hasMiddlemanModel = true ;
58+ $ middlemanModel = $ pivotClass ;
59+ }
60+
61+ $ middlemanTable = $ relationship ->getTable ();
62+ }else if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \HasManyThrough){
63+ $ isManyToMany = true ;
5664 $ hasMiddlemanModel = true ;
57- $ middlemanModel = $ pivotClass ;
65+ $ middlemanModel = get_class ($ relationship ->getParent ());
66+ $ middlemanTable = $ relationship ->getParent ()->getTable ();
5867 }
59-
60- $ middlemanTable = $ relationship ->getTable ();
61- }else if ($ relationship instanceof \Illuminate \Database \Eloquent \Relations \HasManyThrough){
62- $ isManyToMany = true ;
63- $ hasMiddlemanModel = true ;
64- $ middlemanModel = get_class ($ relationship ->getParent ());
65- $ middlemanTable = $ relationship ->getParent ()->getTable ();
68+
69+ $ carry [$ method ->name ] = [
70+ 'relationship_class ' => (new \ReflectionClass ((string ) $ returnType ))->getName (),
71+ 'short_relationship_class ' => (new \ReflectionClass ((string ) $ returnType ))->getShortName (),
72+ 'relationship_model ' => get_class ($ relationship ->getRelated ()),
73+ 'relationship_table ' => $ relationshipTable ,
74+ 'is_many_to_many ' => $ isManyToMany ,
75+ 'has_middleman ' => $ hasMiddlemanModel ,
76+ 'middleman_model ' => $ middlemanModel ,
77+ 'middleman_table ' => $ middlemanTable ,
78+ ];
6679 }
80+ } catch (\Exception $ e ) {
6781
68- $ carry [$ method ->name ] = [
69- 'relationship_class ' => (new \ReflectionClass ((string ) $ returnType ))->getName (),
70- 'short_relationship_class ' => (new \ReflectionClass ((string ) $ returnType ))->getShortName (),
71- 'relationship_model ' => get_class ($ relationship ->getRelated ()),
72- 'relationship_table ' => $ relationshipTable ,
73- 'is_many_to_many ' => $ isManyToMany ,
74- 'has_middleman ' => $ hasMiddlemanModel ,
75- 'middleman_model ' => $ middlemanModel ,
76- 'middleman_table ' => $ middlemanTable ,
77- ];
7882 }
7983
8084 return $ carry ;
0 commit comments