Skip to content

Commit dc70337

Browse files
pataarlaravel-ide-helper
andauthored
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 37559ab. * resolve namespaces --------- Co-authored-by: laravel-ide-helper <[email protected]>
1 parent 4b051e4 commit dc70337

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.1.0...master)
5+
[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v3.2.0...master)
6+
--------------
7+
8+
### Changed
9+
Add support for multiple pivot types when using the same accessor.
10+
11+
2024-10-18, 3.2.0
612
--------------
713

814
### Fixed

src/Console/ModelsCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,25 @@ public function getPropertiesFromMethods($model)
714714
if ($relationObj instanceof BelongsToMany) {
715715
$pivot = get_class($relationObj->newPivot());
716716
if (!in_array($pivot, [Pivot::class, MorphPivot::class])) {
717+
$pivot = $this->getClassNameInDestinationFile($model, $pivot);
718+
719+
if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) {
720+
// If the pivot is already set, we need to append the type to it
721+
$pivot .= '|' . $existingPivot['type'];
722+
} else {
723+
// pivots are not always set
724+
$pivot .= '|null';
725+
}
726+
717727
$this->setProperty(
718728
$relationObj->getPivotAccessor(),
719-
$this->getClassNameInDestinationFile($model, $pivot),
729+
$pivot,
720730
true,
721731
false
722732
);
723733
}
724734
}
735+
725736
//Collection or array of models (because Collection is Arrayable)
726737
$relatedClass = '\\' . get_class($relationObj->getRelated());
727738
$collectionClass = $this->getCollectionClass($relatedClass);

tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models;
66

77
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\DifferentCustomPivot;
89
use Illuminate\Database\Eloquent\Model;
910

1011
class ModelWithPivot extends Model
@@ -15,4 +16,26 @@ public function relationWithCustomPivot()
1516
->using(CustomPivot::class)
1617
->as('customAccessor');
1718
}
19+
20+
21+
public function relationWithDifferentCustomPivot()
22+
{
23+
return $this->belongsToMany(ModelwithPivot::class)
24+
->using(DifferentCustomPivot::class)
25+
->as('differentCustomAccessor');
26+
}
27+
28+
// without an accessor
29+
30+
public function relationCustomPivotUsingSameAccessor()
31+
{
32+
return $this->belongsToMany(ModelwithPivot::class)
33+
->using(CustomPivot::class);
34+
}
35+
36+
public function relationWithDifferentCustomPivotUsingSameAccessor()
37+
{
38+
return $this->belongsToMany(ModelwithPivot::class)
39+
->using(DifferentCustomPivot::class);
40+
}
1841
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;
6+
7+
use Illuminate\Database\Eloquent\Relations\Pivot;
8+
9+
class DifferentCustomPivot extends Pivot
10+
{
11+
}

tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models;
66

77
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\DifferentCustomPivot;
89
use Illuminate\Database\Eloquent\Model;
910

1011
/**
1112
*
1213
*
13-
* @property-read CustomPivot $customAccessor
14+
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
15+
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
16+
* @property-read int|null $relation_custom_pivot_using_same_accessor_count
17+
* @property-read CustomPivot|null $customAccessor
1418
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
1519
* @property-read int|null $relation_with_custom_pivot_count
20+
* @property-read DifferentCustomPivot|null $differentCustomAccessor
21+
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivot
22+
* @property-read int|null $relation_with_different_custom_pivot_count
23+
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivotUsingSameAccessor
24+
* @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count
1625
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newModelQuery()
1726
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newQuery()
1827
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot query()
@@ -26,6 +35,28 @@ public function relationWithCustomPivot()
2635
->using(CustomPivot::class)
2736
->as('customAccessor');
2837
}
38+
39+
40+
public function relationWithDifferentCustomPivot()
41+
{
42+
return $this->belongsToMany(ModelwithPivot::class)
43+
->using(DifferentCustomPivot::class)
44+
->as('differentCustomAccessor');
45+
}
46+
47+
// without an accessor
48+
49+
public function relationCustomPivotUsingSameAccessor()
50+
{
51+
return $this->belongsToMany(ModelwithPivot::class)
52+
->using(CustomPivot::class);
53+
}
54+
55+
public function relationWithDifferentCustomPivotUsingSameAccessor()
56+
{
57+
return $this->belongsToMany(ModelwithPivot::class)
58+
->using(DifferentCustomPivot::class);
59+
}
2960
}
3061
<?php
3162

@@ -46,3 +77,22 @@ public function relationWithCustomPivot()
4677
class CustomPivot extends Pivot
4778
{
4879
}
80+
<?php
81+
82+
declare(strict_types=1);
83+
84+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;
85+
86+
use Illuminate\Database\Eloquent\Relations\Pivot;
87+
88+
/**
89+
*
90+
*
91+
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot newModelQuery()
92+
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot newQuery()
93+
* @method static \Illuminate\Database\Eloquent\Builder<static>|DifferentCustomPivot query()
94+
* @mixin \Eloquent
95+
*/
96+
class DifferentCustomPivot extends Pivot
97+
{
98+
}

0 commit comments

Comments
 (0)