Skip to content

Commit

Permalink
feat(pivot): add support for multiple pivot types when using the same…
Browse files Browse the repository at this point in the history
… 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 37559ab.

* resolve namespaces

---------

Co-authored-by: laravel-ide-helper <[email protected]>
  • Loading branch information
pataar and laravel-ide-helper authored Oct 28, 2024
1 parent 4b051e4 commit dc70337
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
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
{
}

0 comments on commit dc70337

Please sign in to comment.