2
2
3
3
use Illuminate \Database \Eloquent \Relations \MorphMany ;
4
4
use Illuminate \Database \Eloquent \Relations \MorphOne ;
5
+ use Illuminate \Support \Str ;
5
6
use Jenssegers \Mongodb \Model ;
6
7
use Jenssegers \Mongodb \Relations \BelongsTo ;
7
8
use Jenssegers \Mongodb \Relations \BelongsToMany ;
@@ -135,10 +136,10 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
135
136
{
136
137
// If no relation name was given, we will use this debug backtrace to extract
137
138
// 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 .
139
140
if (is_null ($ relation ))
140
141
{
141
- list (, $ caller ) = debug_backtrace (false );
142
+ list ($ current , $ caller ) = debug_backtrace (false , 2 );
142
143
143
144
$ relation = $ caller ['function ' ];
144
145
}
@@ -154,7 +155,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
154
155
// when combined with an "_id" should conventionally match the columns.
155
156
if (is_null ($ foreignKey ))
156
157
{
157
- $ foreignKey = snake_case ($ relation ) . '_id ' ;
158
+ $ foreignKey = Str:: snake ($ relation ) . '_id ' ;
158
159
}
159
160
160
161
$ instance = new $ related ;
@@ -184,9 +185,9 @@ public function morphTo($name = null, $type = null, $id = null)
184
185
// use that to get both the class and foreign key that will be utilized.
185
186
if (is_null ($ name ))
186
187
{
187
- list (, $ caller ) = debug_backtrace (false );
188
+ list ($ current , $ caller ) = debug_backtrace (false , 2 );
188
189
189
- $ name = snake_case ($ caller ['function ' ]);
190
+ $ name = Str:: snake ($ caller ['function ' ]);
190
191
}
191
192
192
193
list ($ type , $ id ) = $ this ->getMorphs ($ name , $ type , $ id );
@@ -201,15 +202,17 @@ public function morphTo($name = null, $type = null, $id = null)
201
202
);
202
203
}
203
204
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
205
206
// as a belongs-to style relationship since morph-to extends that class and
206
207
// we will pass in the appropriate values so that it behaves as expected.
207
208
else
208
209
{
210
+ $ class = $ this ->getActualClassNameForMorph ($ class );
211
+
209
212
$ instance = new $ class ;
210
213
211
214
return new MorphTo (
212
- with ( $ instance) ->newQuery (), $ this , $ id , $ instance ->getKeyName (), $ type , $ name
215
+ $ instance ->newQuery (), $ this , $ id , $ instance ->getKeyName (), $ type , $ name
213
216
);
214
217
}
215
218
}
0 commit comments