Skip to content

Commit 07e3bd8

Browse files
pataarlaravel-ide-helper
and
laravel-ide-helper
authoredOct 29, 2024··
fix(pivot): only use unique classes in the pivot union (Fixes #1606) (#1607)
* fix(pivot): only use unique classes in the pivot union (Fixes #1606) * composer fix-style --------- Co-authored-by: laravel-ide-helper <laravel-ide-helper@users.noreply.github.com>
1 parent 7c2fa01 commit 07e3bd8

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
 

‎src/Console/ModelsCommand.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ public function getPropertiesFromMethods($model)
720720
$relationReturnType === 'many' ||
721721
(
722722
!$relationReturnType &&
723-
strpos(get_class($relationObj), 'Many') !== false
723+
str_contains(get_class($relationObj), 'Many')
724724
)
725725
) {
726726
if ($relationObj instanceof BelongsToMany) {
@@ -729,16 +729,22 @@ 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+
$existingClasses = explode('|', $existingPivot['type']);
733+
734+
if (!in_array($pivot, $existingClasses)) {
735+
array_unshift($existingClasses, $pivot);
736+
}
734737
} else {
735-
// pivots are not always set
736-
$pivot .= '|null';
738+
// No existing pivot property, so we need to add a null type
739+
$existingClasses = [$pivot, 'null'];
737740
}
738741

742+
// create a union type of all pivot classes
743+
$unionType = implode('|', $existingClasses);
744+
739745
$this->setProperty(
740746
$relationObj->getPivotAccessor(),
741-
$pivot,
747+
$unionType,
742748
true,
743749
false
744750
);

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

+6
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

+8
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)
Please sign in to comment.