Skip to content

Commit 4c66ad5

Browse files
add an argument to pass the desired relation name on morphToMany and morphedByMany (#41490)
1 parent b05f89a commit 4c66ad5

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,14 +536,18 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
536536
* @param string|null $relatedPivotKey
537537
* @param string|null $parentKey
538538
* @param string|null $relatedKey
539+
* @param string|null $relation
539540
* @param bool $inverse
540541
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
541542
*/
542543
public function morphToMany($related, $name, $table = null, $foreignPivotKey = null,
543544
$relatedPivotKey = null, $parentKey = null,
544-
$relatedKey = null, $inverse = false)
545+
$relatedKey = null, $relation = null, $inverse = false)
545546
{
546-
$caller = $this->guessBelongsToManyRelation();
547+
// If no relationship name was passed, we will pull backtraces to get the
548+
// name of the calling function. We will use that function name as the
549+
// title of this relation since that is a great convention to apply.
550+
$relation = $relation ?: $this->guessBelongsToManyRelation();
547551

548552
// First, we will need to determine the foreign key and "other key" for the
549553
// relationship. Once we have determined the keys we will make the query
@@ -568,7 +572,7 @@ public function morphToMany($related, $name, $table = null, $foreignPivotKey = n
568572
return $this->newMorphToMany(
569573
$instance->newQuery(), $this, $name, $table,
570574
$foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(),
571-
$relatedKey ?: $instance->getKeyName(), $caller, $inverse
575+
$relatedKey ?: $instance->getKeyName(), $relation, $inverse
572576
);
573577
}
574578

@@ -605,10 +609,11 @@ protected function newMorphToMany(Builder $query, Model $parent, $name, $table,
605609
* @param string|null $relatedPivotKey
606610
* @param string|null $parentKey
607611
* @param string|null $relatedKey
612+
* @param string|null $relation
608613
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
609614
*/
610615
public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null,
611-
$relatedPivotKey = null, $parentKey = null, $relatedKey = null)
616+
$relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null)
612617
{
613618
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
614619

@@ -619,7 +624,7 @@ public function morphedByMany($related, $name, $table = null, $foreignPivotKey =
619624

620625
return $this->morphToMany(
621626
$related, $name, $table, $foreignPivotKey,
622-
$relatedPivotKey, $parentKey, $relatedKey, true
627+
$relatedPivotKey, $parentKey, $relatedKey, $relation, true
623628
);
624629
}
625630

0 commit comments

Comments
 (0)