Skip to content

Commit 970a10a

Browse files
committed
fix(pivot): only use unique classes in the pivot union (Fixes #1606)
1 parent 75b05a9 commit 970a10a

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Console/ModelsCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,13 @@ public function getPropertiesFromMethods($model)
729729
$pivot = $this->getClassNameInDestinationFile($model, $pivot);
730730

731731
if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) {
732-
// If the pivot is already set, we need to append the type to it
733-
$pivot .= '|' . $existingPivot['type'];
732+
if(!str_contains($existingPivot['type'], $pivot . '|')) {
733+
// If the pivot is already set, we need to append the type to it
734+
$pivot .= '|' . $existingPivot['type'];
735+
} else {
736+
// this class is already set
737+
$pivot = $existingPivot['type'];
738+
}
734739
} else {
735740
// pivots are not always set
736741
$pivot .= '|null';

tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public function relationCustomPivotUsingSameAccessor()
3333
->using(CustomPivot::class);
3434
}
3535

36+
public function relationCustomPivotUsingSameAccessorAndClass()
37+
{
38+
return $this->belongsToMany(ModelwithPivot::class)
39+
->using(CustomPivot::class);
40+
}
41+
3642
public function relationWithDifferentCustomPivotUsingSameAccessor()
3743
{
3844
return $this->belongsToMany(ModelwithPivot::class)

tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
1515
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
1616
* @property-read int|null $relation_custom_pivot_using_same_accessor_count
17+
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessorAndClass
18+
* @property-read int|null $relation_custom_pivot_using_same_accessor_and_class_count
1719
* @property-read CustomPivot|null $customAccessor
1820
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
1921
* @property-read int|null $relation_with_custom_pivot_count
@@ -52,6 +54,12 @@ public function relationCustomPivotUsingSameAccessor()
5254
->using(CustomPivot::class);
5355
}
5456

57+
public function relationCustomPivotUsingSameAccessorAndClass()
58+
{
59+
return $this->belongsToMany(ModelwithPivot::class)
60+
->using(CustomPivot::class);
61+
}
62+
5563
public function relationWithDifferentCustomPivotUsingSameAccessor()
5664
{
5765
return $this->belongsToMany(ModelwithPivot::class)

0 commit comments

Comments
 (0)