From dc70337e48c668d077c744e7ad4fc9e355f8557b Mon Sep 17 00:00:00 2001 From: Pieter Willekens Date: Mon, 28 Oct 2024 11:32:19 +0100 Subject: [PATCH] feat(pivot): add support for multiple pivot types when using the same accessor (#1597) * feat(pivot): add support for multiple pivot types when using the same accessor * composer fix-style * changelog * resolve namespaces * Revert "resolve namespaces" This reverts commit 37559ab089b5e21bc7c227eeca0734cffb492e69. * resolve namespaces --------- Co-authored-by: laravel-ide-helper --- CHANGELOG.md | 8 ++- src/Console/ModelsCommand.php | 13 ++++- .../Pivot/Models/ModelWithPivot.php | 23 ++++++++ .../Models/Pivots/DifferentCustomPivot.php | 11 ++++ .../Pivot/__snapshots__/Test__test__1.php | 52 ++++++++++++++++++- 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/Console/ModelsCommand/Pivot/Models/Pivots/DifferentCustomPivot.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 039bc40..f8a2643 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 4d3af98..cab4392 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -714,14 +714,25 @@ class ModelsCommand extends Command 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 3f9e0bd..a01adb3 100644 --- a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -5,6 +5,7 @@ declare(strict_types=1); 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 @@ class ModelWithPivot extends Model ->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 0000000..71a6426 --- /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 @@ class ModelWithPivot extends Model ->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 +{ +}