Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pivot): add support for multiple pivot types when using the same accessor #1597

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;

use Illuminate\Database\Eloquent\Relations\Pivot;

class DifferentCustomPivot extends Pivot
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
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;

/**
*
*
* @property-read CustomPivot $customAccessor
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
* @property-read int|null $relation_custom_pivot_using_same_accessor_count
* @property-read CustomPivot|null $customAccessor
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
* @property-read int|null $relation_with_custom_pivot_count
* @property-read DifferentCustomPivot|null $differentCustomAccessor
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivot
* @property-read int|null $relation_with_different_custom_pivot_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivotUsingSameAccessor
* @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot query()
Expand All @@ -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);
}
}
<?php

Expand All @@ -46,3 +77,22 @@ public function relationWithCustomPivot()
class CustomPivot extends Pivot
{
}
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;

use Illuminate\Database\Eloquent\Relations\Pivot;

/**
*
*
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot query()
* @mixin \Eloquent
*/
class DifferentCustomPivot extends Pivot
{
}