diff --git a/CHANGELOG.md b/CHANGELOG.md index 039bc4072..f8a2643d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ All notable changes to this project will be documented in this file. -[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.1.0...master) +[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.2.0...master) +-------------- + +### Changed +Add support for multiple pivot types when using the same accessor. + +2024-10-18, 3.2.0 -------------- ### Fixed diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index de834bd14..9cc287d69 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -714,14 +714,25 @@ public function getPropertiesFromMethods($model) if ($relationObj instanceof BelongsToMany) { $pivot = get_class($relationObj->newPivot()); if (!in_array($pivot, [Pivot::class, MorphPivot::class])) { + $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']; + } else { + // pivots are not always set + $pivot .= '|null'; + } + $this->setProperty( $relationObj->getPivotAccessor(), - $this->getClassNameInDestinationFile($model, $pivot), + $pivot, true, false ); } } + //Collection or array of models (because Collection is Arrayable) $relatedClass = '\\' . get_class($relationObj->getRelated()); $collectionClass = $this->getCollectionClass($relatedClass); diff --git a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php index 3f9e0bd2c..a01adb325 100644 --- a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -5,6 +5,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\DifferentCustomPivot; use Illuminate\Database\Eloquent\Model; class ModelWithPivot extends Model @@ -15,4 +16,26 @@ public function relationWithCustomPivot() ->using(CustomPivot::class) ->as('customAccessor'); } + + + public function relationWithDifferentCustomPivot() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class) + ->as('differentCustomAccessor'); + } + + // without an accessor + + public function relationCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class); + } + + public function relationWithDifferentCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class); + } } diff --git a/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php b/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php new file mode 100644 index 000000000..71a642657 --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php @@ -0,0 +1,11 @@ + $relationCustomPivotUsingSameAccessor + * @property-read int|null $relation_custom_pivot_using_same_accessor_count + * @property-read CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count + * @property-read DifferentCustomPivot|null $differentCustomAccessor + * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot + * @property-read int|null $relation_with_different_custom_pivot_count + * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor + * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot query() @@ -26,6 +35,28 @@ public function relationWithCustomPivot() ->using(CustomPivot::class) ->as('customAccessor'); } + + + public function relationWithDifferentCustomPivot() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class) + ->as('differentCustomAccessor'); + } + + // without an accessor + + public function relationCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class); + } + + public function relationWithDifferentCustomPivotUsingSameAccessor() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(DifferentCustomPivot::class); + } } |DifferentCustomPivot newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|DifferentCustomPivot newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|DifferentCustomPivot query() + * @mixin \Eloquent + */ +class DifferentCustomPivot extends Pivot +{ +}