diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d930cdf..021d449 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -720,7 +720,7 @@ class ModelsCommand extends Command $relationReturnType === 'many' || ( !$relationReturnType && - strpos(get_class($relationObj), 'Many') !== false + str_contains(get_class($relationObj), 'Many') ) ) { if ($relationObj instanceof BelongsToMany) { @@ -729,16 +729,22 @@ class ModelsCommand extends Command $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 a01adb3..45040fb 100644 --- a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -33,6 +33,12 @@ class ModelWithPivot extends Model ->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 b51b53d..b1dd876 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 @@ use Illuminate\Database\Eloquent\Model; * @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 @@ class ModelWithPivot extends Model ->using(CustomPivot::class); } + public function relationCustomPivotUsingSameAccessorAndClass() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class); + } + public function relationWithDifferentCustomPivotUsingSameAccessor() { return $this->belongsToMany(ModelwithPivot::class)