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 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d2d484ff5..5285fb0e5 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -34,8 +34,10 @@ 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; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Schema\Builder; use Illuminate\Filesystem\Filesystem; @@ -708,6 +710,17 @@ public function getPropertiesFromMethods($model) strpos(get_class($relationObj), 'Many') !== false ) ) { + if ($relationObj instanceof BelongsToMany) { + $pivot = get_class($relationObj->newPivot()); + if (!in_array($pivot,[ Pivot::class, MorphPivot::class])) { + $this->setProperty( + $relationObj->getPivotAccessor(), + $this->getClassNameInDestinationFile($model,$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 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..d9363d272 --- /dev/null +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -0,0 +1,45 @@ + $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'); + } +} +