From 9e178111aa2ddd4f21798a61eb56fd36bd90966b Mon Sep 17 00:00:00 2001 From: pataar Date: Mon, 28 Oct 2024 20:39:52 +0100 Subject: [PATCH 1/2] fix(pivot): only use unique classes in the pivot union (Fixes #1606) --- src/Console/ModelsCommand.php | 18 ++++++++++++------ .../Pivot/Models/ModelWithPivot.php | 6 ++++++ .../Pivot/__snapshots__/Test__test__1.php | 8 ++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d930cdfbe..b124b5b49 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -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) { @@ -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 ); 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) From 96c05e1ef5fee3a608971d467f0e21994cd76f5a Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Mon, 28 Oct 2024 20:14:45 +0000 Subject: [PATCH 2/2] composer fix-style --- src/Console/ModelsCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index b124b5b49..021d449e7 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -731,7 +731,7 @@ public function getPropertiesFromMethods($model) if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) { $existingClasses = explode('|', $existingPivot['type']); - if(!in_array($pivot, $existingClasses)) { + if (!in_array($pivot, $existingClasses)) { array_unshift($existingClasses, $pivot); } } else {