Skip to content

Commit

Permalink
fix(pivot): only use unique classes in the pivot union (Fixes #1606) (#…
Browse files Browse the repository at this point in the history
…1607)

* fix(pivot): only use unique classes in the pivot union (Fixes #1606)

* composer fix-style

---------

Co-authored-by: laravel-ide-helper <[email protected]>
  • Loading branch information
pataar and laravel-ide-helper authored Oct 29, 2024
1 parent 7c2fa01 commit 07e3bd8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ public function getPropertiesFromMethods($model)
$relationReturnType === 'many' ||
(
!$relationReturnType &&
strpos(get_class($relationObj), 'Many') !== false
str_contains(get_class($relationObj), 'Many')
)
) {
if ($relationObj instanceof BelongsToMany) {
Expand All @@ -729,16 +729,22 @@ public function getPropertiesFromMethods($model)
$pivot = $this->getClassNameInDestinationFile($model, $pivot);

if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) {
// If the pivot is already set, we need to append the type to it
$pivot .= '|' . $existingPivot['type'];
$existingClasses = explode('|', $existingPivot['type']);

if (!in_array($pivot, $existingClasses)) {
array_unshift($existingClasses, $pivot);
}
} else {
// pivots are not always set
$pivot .= '|null';
// No existing pivot property, so we need to add a null type
$existingClasses = [$pivot, 'null'];
}

// create a union type of all pivot classes
$unionType = implode('|', $existingClasses);

$this->setProperty(
$relationObj->getPivotAccessor(),
$pivot,
$unionType,
true,
false
);
Expand Down
6 changes: 6 additions & 0 deletions tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function relationCustomPivotUsingSameAccessor()
->using(CustomPivot::class);
}

public function relationCustomPivotUsingSameAccessorAndClass()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class);
}

public function relationWithDifferentCustomPivotUsingSameAccessor()
{
return $this->belongsToMany(ModelwithPivot::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
* @property-read int|null $relation_custom_pivot_using_same_accessor_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessorAndClass
* @property-read int|null $relation_custom_pivot_using_same_accessor_and_class_count
* @property-read CustomPivot|null $customAccessor
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
* @property-read int|null $relation_with_custom_pivot_count
Expand Down Expand Up @@ -52,6 +54,12 @@ public function relationCustomPivotUsingSameAccessor()
->using(CustomPivot::class);
}

public function relationCustomPivotUsingSameAccessorAndClass()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class);
}

public function relationWithDifferentCustomPivotUsingSameAccessor()
{
return $this->belongsToMany(ModelwithPivot::class)
Expand Down

0 comments on commit 07e3bd8

Please sign in to comment.