From d39e7edbcacfd81421712277155a263527a2db60 Mon Sep 17 00:00:00 2001 From: Dev2a Date: Fri, 16 Feb 2024 22:12:40 +0100 Subject: [PATCH 1/5] Add pivot class property --- src/Console/ModelsCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d2d484ff5..42f215e9e 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -36,6 +36,7 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Schema\Builder; use Illuminate\Filesystem\Filesystem; @@ -708,6 +709,17 @@ public function getPropertiesFromMethods($model) strpos(get_class($relationObj), 'Many') !== false ) ) { + if ($relationObj instanceof BelongsToMany) { + $pivot = get_class($relationObj->newPivot()); + if ($pivot != Pivot::class) { + $this->setProperty( + $relationObj->getPivotAccessor(), + $pivot, + true, + false + ); + } + } //Collection or array of models (because Collection is Arrayable) $relatedClass = '\\' . get_class($relationObj->getRelated()); $collectionClass = $this->getCollectionClass($relatedClass); From ad631f3828fe46ee497604b165fb2995f679845f Mon Sep 17 00:00:00 2001 From: Dev2a Date: Fri, 16 Feb 2024 23:30:13 +0100 Subject: [PATCH 2/5] Add test for pivot class properties --- src/Console/ModelsCommand.php | 2 +- .../Pivot/Models/ModelWithPivot.php | 16 +++++++ .../Pivot/Models/Pivots/CustomPivot.php | 10 ++++ tests/Console/ModelsCommand/Pivot/Test.php | 22 +++++++++ .../Pivot/__snapshots__/Test__test__1.php | 46 +++++++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php create mode 100644 tests/Console/ModelsCommand/Pivot/Models/Pivots/CustomPivot.php create mode 100644 tests/Console/ModelsCommand/Pivot/Test.php create mode 100644 tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 42f215e9e..d67623d7c 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -714,7 +714,7 @@ public function getPropertiesFromMethods($model) if ($pivot != Pivot::class) { $this->setProperty( $relationObj->getPivotAccessor(), - $pivot, + $this->getClassNameInDestinationFile($model,$pivot), true, false ); diff --git a/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php new file mode 100644 index 000000000..f579cf9db --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php @@ -0,0 +1,16 @@ +belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class) + ->as('customAccessor'); + } +} diff --git a/tests/Console/ModelsCommand/Pivot/Models/Pivots/CustomPivot.php b/tests/Console/ModelsCommand/Pivot/Models/Pivots/CustomPivot.php new file mode 100644 index 000000000..d442c501e --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/Models/Pivots/CustomPivot.php @@ -0,0 +1,10 @@ +app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php new file mode 100644 index 000000000..435d064e1 --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -0,0 +1,46 @@ + $relationWithCustomPivot + * @property-read int|null $relation_with_custom_pivot_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() + * @mixin \Eloquent + */ +class ModelWithPivot extends Model +{ + public function relationWithCustomPivot() + { + return $this->belongsToMany(ModelwithPivot::class) + ->using(CustomPivot::class) + ->as('customAccessor') + ; + } +} + Date: Sat, 17 Feb 2024 13:15:11 +0100 Subject: [PATCH 3/5] Fix failed test for pivot class properties --- src/Console/ModelsCommand.php | 3 ++- .../ModelsCommand/Pivot/__snapshots__/Test__test__1.php | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d67623d7c..5285fb0e5 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -34,6 +34,7 @@ use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphOne; +use Illuminate\Database\Eloquent\Relations\MorphPivot; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\Pivot; @@ -711,7 +712,7 @@ public function getPropertiesFromMethods($model) ) { if ($relationObj instanceof BelongsToMany) { $pivot = get_class($relationObj->newPivot()); - if ($pivot != Pivot::class) { + if (!in_array($pivot,[ Pivot::class, MorphPivot::class])) { $this->setProperty( $relationObj->getPivotAccessor(), $this->getClassNameInDestinationFile($model,$pivot), diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index 435d064e1..b12262a7a 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -22,8 +22,7 @@ public function relationWithCustomPivot() { return $this->belongsToMany(ModelwithPivot::class) ->using(CustomPivot::class) - ->as('customAccessor') - ; + ->as('customAccessor'); } } Date: Sun, 10 Mar 2024 12:42:41 +0100 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa7543638..7dc6410bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. ### Changed ### Added + - Add type to pivot when using a custom pivot class [#1518 / d3v2a](https://github.com/barryvdh/laravel-ide-helper/pull/1518) 2024-03-01, 3.0.0 ------------------ From 586143c094294a68b910cbbd1245f0823c42ed2c Mon Sep 17 00:00:00 2001 From: Dev2a Date: Sun, 10 Mar 2024 18:00:45 +0100 Subject: [PATCH 5/5] Fix failed test --- .../ModelsCommand/Pivot/__snapshots__/Test__test__1.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index b12262a7a..d9363d272 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; /** - * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\ModelWithPivot + * * * @property-read CustomPivot $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot @@ -32,7 +32,7 @@ public function relationWithCustomPivot() use Illuminate\Database\Eloquent\Relations\Pivot; /** - * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot + * * * @method static \Illuminate\Database\Eloquent\Builder|CustomPivot newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomPivot newQuery()