From 970a10ad357b7b18112204324818e8e4a7388271 Mon Sep 17 00:00:00 2001 From: pataar Date: Mon, 28 Oct 2024 20:39:52 +0100 Subject: [PATCH] fix(pivot): only use unique classes in the pivot union (Fixes #1606) --- src/Console/ModelsCommand.php | 9 +++++++-- .../ModelsCommand/Pivot/Models/ModelWithPivot.php | 6 ++++++ .../ModelsCommand/Pivot/__snapshots__/Test__test__1.php | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d930cdfbe..0261b13d1 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -729,8 +729,13 @@ 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']; + if(!str_contains($existingPivot['type'], $pivot . '|')) { + // If the pivot is already set, we need to append the type to it + $pivot .= '|' . $existingPivot['type']; + } else { + // this class is already set + $pivot = $existingPivot['type']; + } } else { // pivots are not always set $pivot .= '|null'; diff --git a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php index a01adb325..45040fbe9 100644 --- a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -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) diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index b51b53dfd..b1dd87653 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -14,6 +14,8 @@ * @property-read DifferentCustomPivot|CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count + * @property-read \Illuminate\Database\Eloquent\Collection $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 $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count @@ -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)