15
15
use MongoDB \Laravel \Relations \HasOne ;
16
16
use MongoDB \Laravel \Relations \MorphMany ;
17
17
use MongoDB \Laravel \Relations \MorphTo ;
18
+ use MongoDB \Laravel \Relations \MorphToMany ;
19
+
18
20
19
21
use function debug_backtrace ;
20
22
use function is_subclass_of ;
@@ -41,7 +43,7 @@ trait HybridRelations
41
43
public function hasOne ($ related , $ foreignKey = null , $ localKey = null )
42
44
{
43
45
// Check if it is a relation with an original model.
44
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
46
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
45
47
return parent ::hasOne ($ related , $ foreignKey , $ localKey );
46
48
}
47
49
@@ -70,7 +72,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
70
72
public function morphOne ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
71
73
{
72
74
// Check if it is a relation with an original model.
73
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
75
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
74
76
return parent ::morphOne ($ related , $ name , $ type , $ id , $ localKey );
75
77
}
76
78
@@ -97,7 +99,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
97
99
public function hasMany ($ related , $ foreignKey = null , $ localKey = null )
98
100
{
99
101
// Check if it is a relation with an original model.
100
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
102
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
101
103
return parent ::hasMany ($ related , $ foreignKey , $ localKey );
102
104
}
103
105
@@ -126,7 +128,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
126
128
public function morphMany ($ related , $ name , $ type = null , $ id = null , $ localKey = null )
127
129
{
128
130
// Check if it is a relation with an original model.
129
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
131
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
130
132
return parent ::morphMany ($ related , $ name , $ type , $ id , $ localKey );
131
133
}
132
134
@@ -166,7 +168,7 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
166
168
}
167
169
168
170
// Check if it is a relation with an original model.
169
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
171
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
170
172
return parent ::belongsTo ($ related , $ foreignKey , $ ownerKey , $ relation );
171
173
}
172
174
@@ -278,7 +280,7 @@ public function belongsToMany(
278
280
}
279
281
280
282
// Check if it is a relation with an original model.
281
- if (! is_subclass_of ($ related , MongoDBModel::class)) {
283
+ if (!is_subclass_of ($ related , MongoDBModel::class)) {
282
284
return parent ::belongsToMany (
283
285
$ related ,
284
286
$ collection ,
@@ -323,6 +325,139 @@ public function belongsToMany(
323
325
);
324
326
}
325
327
328
+ /**
329
+ * Define a many-to-many relationship.
330
+ *
331
+ * @param string $related
332
+ * @param string $collection
333
+ * @param string $foreignKey
334
+ * @param string $otherKey
335
+ * @param string $parentKey
336
+ * @param string $relatedKey
337
+ * @param string $relation
338
+ * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
339
+ */
340
+ public function morphToMany (
341
+ $ related ,
342
+ $ name ,
343
+ $ table = null ,
344
+ $ foreignPivotKey = null ,
345
+ $ relatedPivotKey = null ,
346
+ $ parentKey = null ,
347
+ $ relatedKey = null ,
348
+ $ relation = null ,
349
+ $ inverse = false
350
+ ) {
351
+
352
+ // Check if it is a relation with an original model.
353
+ if (!is_subclass_of ($ related , \Mongodb \Laravel \Eloquent \Model::class)) {
354
+ return parent ::MorphToMany (
355
+ $ related ,
356
+ $ name ,
357
+ $ table ,
358
+ $ foreignPivotKey ,
359
+ $ relatedPivotKey ,
360
+ $ parentKey ,
361
+ $ relatedKey ,
362
+ $ inverse ,
363
+ );
364
+ }
365
+
366
+ $ caller = $ this ->guessBelongsToManyRelation ();
367
+
368
+ $ instance = new $ related ;
369
+
370
+ $ foreignPivotKey = $ foreignPivotKey ?: $ name . '_id ' ;
371
+
372
+ $ relatedPivotKey = $ relatedPivotKey ?: $ instance ->getForeignKey () . 's ' ;
373
+
374
+ // Now we're ready to create a new query builder for the related model and
375
+ // the relationship instances for this relation. This relation will set
376
+ // appropriate query constraints then entirely manage the hydrations.
377
+ if (!$ table ) {
378
+ $ words = preg_split ('/(_)/u ' , $ name , -1 , PREG_SPLIT_DELIM_CAPTURE );
379
+
380
+ $ lastWord = array_pop ($ words );
381
+
382
+ $ table = implode ('' , $ words ) . Str::plural ($ lastWord );
383
+ }
384
+
385
+ return new MorphToMany (
386
+ $ instance ->newQuery (),
387
+ $ this ,
388
+ $ name ,
389
+ $ table ,
390
+ $ foreignPivotKey ,
391
+ $ relatedPivotKey ,
392
+ $ parentKey ?: $ this ->getKeyName (),
393
+ $ relatedKey ?: $ instance ->getKeyName (),
394
+ $ caller ,
395
+ $ inverse ,
396
+ );
397
+ }
398
+
399
+ /**
400
+ * Define a polymorphic, inverse many-to-many relationship.
401
+ *
402
+ * @param string $related
403
+ * @param string $name
404
+ * @param string|null $table
405
+ * @param string|null $foreignPivotKey
406
+ * @param string|null $relatedPivotKey
407
+ * @param string|null $parentKey
408
+ * @param string|null $relatedKey
409
+ * @param string|null $relation
410
+ * @param bool $inverse
411
+ *
412
+ * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
413
+ */
414
+ public function morphedByMany (
415
+ $ related ,
416
+ $ name ,
417
+ $ table = null ,
418
+ $ foreignPivotKey = null ,
419
+ $ relatedPivotKey = null ,
420
+ $ parentKey = null ,
421
+ $ relatedKey = null ,
422
+ $ inverse = false
423
+ ) {
424
+ $ caller = $ this ->guessBelongsToManyRelation ();
425
+
426
+ // $instance = new $related;
427
+ // For the inverse of the polymorphic many-to-many relations, we will change
428
+ // the way we determine the foreign and other keys, as it is the opposite
429
+ // of the morph-to-many method since we're figuring out these inverses.
430
+ $ relatedPivotKey = $ relatedPivotKey ?: $ name . '_id ' ;
431
+
432
+ $ foreignPivotKey = $ foreignPivotKey ?: $ this ->getForeignKey () . 's ' ;
433
+
434
+ return $ this ->morphToMany (
435
+ $ related ,
436
+ $ name ,
437
+ $ table ,
438
+ $ foreignPivotKey ,
439
+ $ relatedPivotKey ,
440
+ $ parentKey ,
441
+ $ relatedKey ,
442
+ null ,
443
+ true ,
444
+ );
445
+ }
446
+
447
+ /**
448
+ * Get the relationship name of the belongs to many.
449
+ *
450
+ * @return string
451
+ */
452
+ protected function guessBelongsToManyRelation ()
453
+ {
454
+ if (method_exists ($ this , 'getBelongsToManyCaller ' )) {
455
+ return $ this ->getBelongsToManyCaller ();
456
+ }
457
+
458
+ return parent ::guessBelongsToManyRelation ();
459
+ }
460
+
326
461
/** @inheritdoc */
327
462
public function newEloquentBuilder ($ query )
328
463
{
0 commit comments