Completed
Push — master ( 135018...0f0a30 )
by Andrey
04:47
created

HasRelationships::morphInstanceTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * Include relations definitions to eloquent
12
 *
13
 * @package Volosyuk\SimpleEloquent
14
 */
15
trait HasRelationships
16
{
17
    /**
18
     * Define a one-to-one relationship.
19
     *
20
     * @param string $related
21
     * @param string $foreignKey
22
     * @param string $localKey
23
     * @return HasOne
24
     */
25 1
    public function hasOne($related, $foreignKey = null, $localKey = null)
26
    {
27
        /**
28
         * @var Model $instance
29
         */
30 1
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        /** @scrutinizer ignore-call */ 
31
        $instance = $this->newRelatedInstance($related);
Loading history...
31
32 1
        $foreignKey = $foreignKey ?: $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $foreignKey = $foreignKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
33
34 1
        $localKey = $localKey ?: $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
35
36 1
        return $this->newHasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...ationships::newHasOne(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        return $this->newHasOne($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $instance->getTable().'.'.$foreignKey, $localKey);
Loading history...
37
    }
38
39
    /**
40
     * Instantiate a new HasOne relationship.
41
     *
42
     * @param Builder $query
43
     * @param Model $parent
44
     * @param string $foreignKey
45
     * @param string $localKey
46
     * @return HasOne
47
     */
48 1
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
49
    {
50 1
        return new HasOne($query, $parent, $foreignKey, $localKey);
51
    }
52
53
    /**
54
     * Define a polymorphic one-to-one relationship.
55
     *
56
     * @param string $related
57
     * @param string $name
58
     * @param string $type
59
     * @param string $id
60
     * @param string $localKey
61
     * @return MorphOne
62
     */
63 1
    public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
64
    {
65
        /**
66
         * @var Model $instance
67
         */
68 1
        $instance = $this->newRelatedInstance($related);
69
70 1
        list($type, $id) = $this->getMorphs($name, $type, $id);
0 ignored issues
show
Bug introduced by
It seems like getMorphs() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        /** @scrutinizer ignore-call */ 
71
        list($type, $id) = $this->getMorphs($name, $type, $id);
Loading history...
71
72 1
        $table = $instance->getTable();
73
74 1
        $localKey = $localKey ?: $this->getKeyName();
75
76 1
        return $this->newMorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...ionships::newMorphOne(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return $this->newMorphOne($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $table.'.'.$type, $table.'.'.$id, $localKey);
Loading history...
77
    }
78
79
    /**
80
     * Instantiate a new MorphOne relationship.
81
     *
82
     * @param Builder $query
83
     * @param Model $parent
84
     * @param string $type
85
     * @param string $id
86
     * @param string $localKey
87
     * @return MorphOne
88
     */
89 1
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
90
    {
91 1
        return new MorphOne($query, $parent, $type, $id, $localKey);
92
    }
93
94
    /**
95
     * Define an inverse one-to-one or many relationship.
96
     *
97
     * @param string $related
98
     * @param string $foreignKey
99
     * @param string $ownerKey
100
     * @param string $relation
101
     * @return BelongsTo
102
     */
103 1
    public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
104
    {
105 1
        if (is_null($relation)) {
106 1
            $relation = $this->guessBelongsToRelation();
107
        }
108
109
        /**
110
         * @var Model $instance
111
         */
112 1
        $instance = $this->newRelatedInstance($related);
113
114 1
        if (is_null($foreignKey)) {
115 1
            $foreignKey = Str::snake($relation).'_'.$instance->getKeyName();
116
        }
117
118 1
        $ownerKey = $ownerKey ?: $instance->getKeyName();
119
120 1
        return $this->newBelongsTo(
121 1
            $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of Volosyuk\SimpleEloquent\...onships::newBelongsTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $foreignKey, $ownerKey, $relation
Loading history...
122
        );
123
    }
124
125
    /**
126
     * Instantiate a new BelongsTo relationship.
127
     *
128
     * @param Builder $query
129
     * @param Model $child
130
     * @param string $foreignKey
131
     * @param string $ownerKey
132
     * @param string $relation
133
     * @return BelongsTo
134
     */
135 1
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
136
    {
137 1
        return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
138
    }
139
140
    /**
141
     * Define a polymorphic, inverse one-to-one or many relationship.
142
     *
143
     * @param string $name
144
     * @param string $type
145
     * @param string $id
146
     * @param string $ownerKey
147
     * @return MorphTo
148
     */
149 1
    public function morphTo($name = null, $type = null, $id = null, $ownerKey = null)
150
    {
151 1
        $name = $name ?: $this->guessBelongsToRelation();
152
153 1
        list($type, $id) = $this->getMorphs(
154 1
            Str::snake($name), $type, $id
155
        );
156
157 1
        return empty($class = $this->{$type})
158 1
            ? $this->morphEagerTo($name, $type, $id, $ownerKey)
159 1
            : $this->morphInstanceTo($class, $name, $type, $id, $ownerKey);
160
    }
161
162
    /**
163
     * Define a polymorphic, inverse one-to-one or many relationship.
164
     *
165
     * @param string $name
166
     * @param string $type
167
     * @param string $id
168
     * @param string $ownerKey
169
     * @return MorphTo
170
     */
171 1
    protected function morphEagerTo($name, $type, $id, $ownerKey)
172
    {
173 1
        return $this->newMorphTo(
174 1
            $this->newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...tionships::newMorphTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
            $this->newQuery()->setEagerLoads([]), /** @scrutinizer ignore-type */ $this, $id, $ownerKey, $type, $name
Loading history...
Bug introduced by
It seems like newQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
            $this->/** @scrutinizer ignore-call */ 
175
                   newQuery()->setEagerLoads([]), $this, $id, $ownerKey, $type, $name
Loading history...
175
        );
176
    }
177
178
    /**
179
     * Define a polymorphic, inverse one-to-one or many relationship.
180
     *
181
     * @param string $target
182
     * @param string $name
183
     * @param string $type
184
     * @param string $id
185
     * @param string $ownerKey
186
     * @return MorphTo
187
     */
188 1
    protected function morphInstanceTo($target, $name, $type, $id, $ownerKey)
189
    {
190
        /**
191
         * @var Model $instance
192
         */
193 1
        $instance = $this->newRelatedInstance(
194 1
            static::getActualClassNameForMorph($target)
195
        );
196
197 1
        return $this->newMorphTo(
198 1
            $instance->newQuery(), $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...tionships::newMorphTo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $id, $ownerKey ?? $instance->getKeyName(), $type, $name
Loading history...
199
        );
200
    }
201
202
    /**
203
     * Instantiate a new MorphTo relationship.
204
     *
205
     * @param Builder $query
206
     * @param Model $parent
207
     * @param string $foreignKey
208
     * @param string $ownerKey
209
     * @param string $type
210
     * @param string $relation
211
     * @return MorphTo
212
     */
213 1
    protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
214
    {
215 1
        return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
216
    }
217
218
    /**
219
     * Retrieve the actual class name for a given morph class.
220
     *
221
     * @param string $class
222
     * @return string
223
     */
224 1
    public static function getActualClassNameForMorph($class)
225
    {
226 1
        return Arr::get(Relation::morphMap() ?: [], $class, $class);
227
    }
228
229
    /**
230
     * Guess the "belongs to" relationship name.
231
     *
232
     * @return string
233
     */
234 1
    protected function guessBelongsToRelation()
235
    {
236 1
        list($one, $two, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
237
238 1
        return $caller['function'];
239
    }
240
241
    /**
242
     * Define a one-to-many relationship.
243
     *
244
     * @param string $related
245
     * @param string $foreignKey
246
     * @param string $localKey
247
     * @return HasMany
248
     */
249 1
    public function hasMany($related, $foreignKey = null, $localKey = null)
250
    {
251
        /**
252
         * @var Model $instance
253
         */
254 1
        $instance = $this->newRelatedInstance($related);
255
256 1
        $foreignKey = $foreignKey ?: $this->getForeignKey();
257
258 1
        $localKey = $localKey ?: $this->getKeyName();
259
260 1
        return $this->newHasMany(
261 1
            $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...tionships::newHasMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

261
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $instance->getTable().'.'.$foreignKey, $localKey
Loading history...
262
        );
263
    }
264
265
    /**
266
     * Instantiate a new HasMany relationship.
267
     *
268
     * @param Builder $query
269
     * @param Model $parent
270
     * @param string $foreignKey
271
     * @param string $localKey
272
     * @return HasMany
273
     */
274 1
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
275
    {
276 1
        return new HasMany($query, $parent, $foreignKey, $localKey);
277
    }
278
279
    /**
280
     * Define a has-many-through relationship.
281
     *
282
     * @param string $related
283
     * @param string $through
284
     * @param string|null $firstKey
285
     * @param string|null $secondKey
286
     * @param string|null $localKey
287
     * @param string|null $secondLocalKey
288
     * @return HasManyThrough
289
     */
290 1
    public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
291
    {
292
        /**
293
         * @var Model $through
294
         */
295 1
        $through = new $through;
296
297 1
        $firstKey = $firstKey ?: $this->getForeignKey();
298
299 1
        $secondKey = $secondKey ?: $through->getForeignKey();
300
301 1
        return $this->newHasManyThrough(
302 1
            $this->newRelatedInstance($related)->newQuery(), $this, $through,
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $farParent of Volosyuk\SimpleEloquent\...ps::newHasManyThrough(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

302
            $this->newRelatedInstance($related)->newQuery(), /** @scrutinizer ignore-type */ $this, $through,
Loading history...
303 1
            $firstKey, $secondKey, $localKey ?: $this->getKeyName(),
304 1
            $secondLocalKey ?: $through->getKeyName()
305
        );
306
    }
307
308
    /**
309
     * Instantiate a new HasManyThrough relationship.
310
     *
311
     * @param Builder $query
312
     * @param Model $farParent
313
     * @param Model $throughParent
314
     * @param string $firstKey
315
     * @param string $secondKey
316
     * @param string $localKey
317
     * @param string $secondLocalKey
318
     * @return HasManyThrough
319
     */
320 1
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
321
    {
322 1
        return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
323
    }
324
325
    /**
326
     * Define a polymorphic one-to-many relationship.
327
     *
328
     * @param string $related
329
     * @param string $name
330
     * @param string $type
331
     * @param string $id
332
     * @param string $localKey
333
     * @return MorphMany
334
     */
335 1
    public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
336
    {
337
        /**
338
         * @var Model $instance
339
         */
340 1
        $instance = $this->newRelatedInstance($related);
341
342 1
        list($type, $id) = $this->getMorphs($name, $type, $id);
343
344 1
        $table = $instance->getTable();
345
346 1
        $localKey = $localKey ?: $this->getKeyName();
347
348 1
        return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...onships::newMorphMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

348
        return $this->newMorphMany($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $table.'.'.$type, $table.'.'.$id, $localKey);
Loading history...
349
    }
350
351
    /**
352
     * Instantiate a new MorphMany relationship.
353
     *
354
     * @param Builder $query
355
     * @param Model $parent
356
     * @param string $type
357
     * @param string $id
358
     * @param string $localKey
359
     * @return MorphMany
360
     */
361 1
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
362
    {
363 1
        return new MorphMany($query, $parent, $type, $id, $localKey);
364
    }
365
366
    /**
367
     * Define a many-to-many relationship.
368
     *
369
     * @param string $related
370
     * @param string $table
371
     * @param string $foreignPivotKey
372
     * @param string $relatedPivotKey
373
     * @param string $parentKey
374
     * @param string $relatedKey
375
     * @param string $relation
376
     * @return BelongsToMany
377
     */
378 1
    public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
379
                                  $parentKey = null, $relatedKey = null, $relation = null)
380
    {
381 1
        if (is_null($relation)) {
382 1
            $relation = $this->guessBelongsToManyRelation();
383
        }
384
385
        /**
386
         * @var Model $instance
387
         */
388 1
        $instance = $this->newRelatedInstance($related);
389
390 1
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
391
392 1
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
393
394 1
        if (is_null($table)) {
395
            $table = $this->joiningTable($related);
396
        }
397
398 1
        return $this->newBelongsToMany(
399 1
            $instance->newQuery(), $this, $table, $foreignPivotKey,
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...ips::newBelongsToMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

399
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $table, $foreignPivotKey,
Loading history...
400 1
            $relatedPivotKey, $parentKey ?: $this->getKeyName(),
401 1
            $relatedKey ?: $instance->getKeyName(), $relation
402
        );
403
    }
404
405
    /**
406
     * Instantiate a new BelongsToMany relationship.
407
     *
408
     * @param Builder $query
409
     * @param Model $parent
410
     * @param string $table
411
     * @param string $foreignPivotKey
412
     * @param string $relatedPivotKey
413
     * @param string $parentKey
414
     * @param string $relatedKey
415
     * @param string $relationName
416
     * @return BelongsToMany
417
     */
418 1
    protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
419
                                        $parentKey, $relatedKey, $relationName = null)
420
    {
421 1
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
422
    }
423
424
    /**
425
     * Define a polymorphic many-to-many relationship.
426
     *
427
     * @param string $related
428
     * @param string $name
429
     * @param string $table
430
     * @param string $foreignPivotKey
431
     * @param string $relatedPivotKey
432
     * @param string $parentKey
433
     * @param string $relatedKey
434
     * @param  bool  $inverse
435
     * @return MorphToMany
436
     */
437 2
    public function morphToMany($related, $name, $table = null, $foreignPivotKey = null,
438
                                $relatedPivotKey = null, $parentKey = null,
439
                                $relatedKey = null, $inverse = false)
440
    {
441 2
        $caller = $this->guessBelongsToManyRelation();
442
443
        /**
444
         * @var Model $instance
445
         */
446 2
        $instance = $this->newRelatedInstance($related);
447
448 2
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
449
450 2
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
451
452 2
        $table = $table ?: Str::plural($name);
453
454 2
        return $this->newMorphToMany(
455 2
            $instance->newQuery(), $this, $name, $table,
0 ignored issues
show
Bug introduced by
$this of type Volosyuk\SimpleEloquent\Relations\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Volosyuk\SimpleEloquent\...ships::newMorphToMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

455
            $instance->newQuery(), /** @scrutinizer ignore-type */ $this, $name, $table,
Loading history...
456 2
            $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(),
457 2
            $relatedKey ?: $instance->getKeyName(), $caller, $inverse
458
        );
459
    }
460
461
    /**
462
     * Instantiate a new HasManyThrough relationship.
463
     *
464
     * @param Builder $query
465
     * @param Model $parent
466
     * @param string $name
467
     * @param string $table
468
     * @param string $foreignPivotKey
469
     * @param string $relatedPivotKey
470
     * @param string $parentKey
471
     * @param string $relatedKey
472
     * @param string $relationName
473
     * @param  bool  $inverse
474
     * @return MorphToMany
475
     */
476 2
    protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
477
                                      $relatedPivotKey, $parentKey, $relatedKey,
478
                                      $relationName = null, $inverse = false)
479
    {
480 2
        return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
481 2
            $relationName, $inverse);
482
    }
483
484
    /**
485
     * Define a polymorphic, inverse many-to-many relationship.
486
     *
487
     * @param string $related
488
     * @param string $name
489
     * @param string $table
490
     * @param string $foreignPivotKey
491
     * @param string $relatedPivotKey
492
     * @param string $parentKey
493
     * @param string $relatedKey
494
     * @return MorphToMany
495
     */
496 1
    public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null,
497
                                  $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
498
    {
499 1
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
500
501 1
        $relatedPivotKey = $relatedPivotKey ?: $name.'_id';
502
503 1
        return $this->morphToMany(
504 1
            $related, $name, $table, $foreignPivotKey,
505 1
            $relatedPivotKey, $parentKey, $relatedKey, true
506
        );
507
    }
508
509
510
    /**
511
     * Get the relationship name of the belongs to many.
512
     *
513
     * @return string
514
     */
515
    protected function guessBelongsToManyRelation()
516
    {
517 3
        $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) {
518 3
            return ! in_array($trace['function'], Model::$manyMethods);
519 3
        });
520
521 3
        return ! is_null($caller) ? $caller['function'] : null;
522
    }
523
524
    /**
525
     * Get the joining table name for a many-to-many relation.
526
     *
527
     * @param string $related
528
     * @return string
529
     */
530
    public function joiningTable($related)
531
    {
532
        $models = [
533
            Str::snake(class_basename($related)),
534
            Str::snake(class_basename($this)),
535
        ];
536
537
        sort($models);
538
539
        return strtolower(implode('_', $models));
540
    }
541
}
542