22
33use Illuminate \Database \Eloquent \Relations \MorphMany ;
44use Illuminate \Database \Eloquent \Relations \MorphOne ;
5+ use Illuminate \Support \Str ;
56use Jenssegers \Mongodb \Model ;
67use Jenssegers \Mongodb \Relations \BelongsTo ;
78use Jenssegers \Mongodb \Relations \BelongsToMany ;
@@ -135,10 +136,10 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
135136 {
136137 // If no relation name was given, we will use this debug backtrace to extract
137138 // the calling method's name and use that as the relationship name as most
138- // of the time this will be what we desire to use for the relatinoships .
139+ // of the time this will be what we desire to use for the relationships .
139140 if (is_null ($ relation ))
140141 {
141- list (, $ caller ) = debug_backtrace (false );
142+ list ($ current , $ caller ) = debug_backtrace (false , 2 );
142143
143144 $ relation = $ caller ['function ' ];
144145 }
@@ -154,7 +155,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
154155 // when combined with an "_id" should conventionally match the columns.
155156 if (is_null ($ foreignKey ))
156157 {
157- $ foreignKey = snake_case ($ relation ) . '_id ' ;
158+ $ foreignKey = Str:: snake ($ relation ) . '_id ' ;
158159 }
159160
160161 $ instance = new $ related ;
@@ -184,9 +185,9 @@ public function morphTo($name = null, $type = null, $id = null)
184185 // use that to get both the class and foreign key that will be utilized.
185186 if (is_null ($ name ))
186187 {
187- list (, $ caller ) = debug_backtrace (false );
188+ list ($ current , $ caller ) = debug_backtrace (false , 2 );
188189
189- $ name = snake_case ($ caller ['function ' ]);
190+ $ name = Str:: snake ($ caller ['function ' ]);
190191 }
191192
192193 list ($ type , $ id ) = $ this ->getMorphs ($ name , $ type , $ id );
@@ -201,15 +202,17 @@ public function morphTo($name = null, $type = null, $id = null)
201202 );
202203 }
203204
204- // If we are not eager loading the relatinship, we will essentially treat this
205+ // If we are not eager loading the relationship we will essentially treat this
205206 // as a belongs-to style relationship since morph-to extends that class and
206207 // we will pass in the appropriate values so that it behaves as expected.
207208 else
208209 {
210+ $ class = $ this ->getActualClassNameForMorph ($ class );
211+
209212 $ instance = new $ class ;
210213
211214 return new MorphTo (
212- with ( $ instance) ->newQuery (), $ this , $ id , $ instance ->getKeyName (), $ type , $ name
215+ $ instance ->newQuery (), $ this , $ id , $ instance ->getKeyName (), $ type , $ name
213216 );
214217 }
215218 }
0 commit comments