@@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null)
214
214
* @param string $collection
215
215
* @param string $foreignKey
216
216
* @param string $otherKey
217
+ * @param string $parentKey
218
+ * @param string $relatedKey
217
219
* @param string $relation
218
220
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
219
221
*/
220
- public function belongsToMany ($ related , $ collection = null , $ foreignKey = null , $ otherKey = null , $ relation = null )
221
- {
222
+ public function belongsToMany (
223
+ $ related ,
224
+ $ collection = null ,
225
+ $ foreignKey = null ,
226
+ $ otherKey = null ,
227
+ $ parentKey = null ,
228
+ $ relatedKey = null ,
229
+ $ relation = null
230
+ ) {
222
231
// If no relationship name was passed, we will pull backtraces to get the
223
232
// name of the calling function. We will use that function name as the
224
233
// title of this relation since that is a great convention to apply.
@@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
228
237
229
238
// Check if it is a relation with an original model.
230
239
if (!is_subclass_of ($ related , \Jenssegers \Mongodb \Eloquent \Model::class)) {
231
- return parent ::belongsToMany ($ related , $ collection , $ foreignKey , $ otherKey , $ relation );
240
+ return parent ::belongsToMany (
241
+ $ related ,
242
+ $ collection ,
243
+ $ foreignKey ,
244
+ $ otherKey ,
245
+ $ parentKey ,
246
+ $ relatedKey ,
247
+ $ relation
248
+ );
232
249
}
233
250
234
251
// First, we'll need to determine the foreign key and "other key" for the
@@ -252,7 +269,16 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
252
269
// appropriate query constraint and entirely manages the hydrations.
253
270
$ query = $ instance ->newQuery ();
254
271
255
- return new BelongsToMany ($ query , $ this , $ collection , $ foreignKey , $ otherKey , $ relation );
272
+ return new BelongsToMany (
273
+ $ query ,
274
+ $ this ,
275
+ $ collection ,
276
+ $ foreignKey ,
277
+ $ otherKey ,
278
+ $ parentKey ?: $ this ->getKeyName (),
279
+ $ relatedKey ?: $ instance ->getKeyName (),
280
+ $ relation
281
+ );
256
282
}
257
283
258
284
/**
@@ -274,6 +300,10 @@ protected function guessBelongsToManyRelation()
274
300
*/
275
301
public function newEloquentBuilder ($ query )
276
302
{
277
- return new EloquentBuilder ($ query );
303
+ if (is_subclass_of ($ this , \Jenssegers \Mongodb \Eloquent \Model::class)) {
304
+ return new Builder ($ query );
305
+ } else {
306
+ return new EloquentBuilder ($ query );
307
+ }
278
308
}
279
309
}
0 commit comments