Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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