Skip to content

Commit ac7e186

Browse files
authored
Add pivot class property (#1518)
* Add pivot class property * Add test for pivot class properties * Fix failed test for pivot class properties * Update CHANGELOG.md * Fix failed test
1 parent bc1d67f commit ac7e186

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
1010
### Changed
1111

1212
### Added
13+
- Add type to pivot when using a custom pivot class [#1518 / d3v2a](https://github.com/barryvdh/laravel-ide-helper/pull/1518)
1314

1415
2024-03-01, 3.0.0
1516
------------------

src/Console/ModelsCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
3535
use Illuminate\Database\Eloquent\Relations\MorphMany;
3636
use Illuminate\Database\Eloquent\Relations\MorphOne;
37+
use Illuminate\Database\Eloquent\Relations\MorphPivot;
3738
use Illuminate\Database\Eloquent\Relations\MorphTo;
3839
use Illuminate\Database\Eloquent\Relations\MorphToMany;
40+
use Illuminate\Database\Eloquent\Relations\Pivot;
3941
use Illuminate\Database\Eloquent\Relations\Relation;
4042
use Illuminate\Database\Schema\Builder;
4143
use Illuminate\Filesystem\Filesystem;
@@ -708,6 +710,17 @@ public function getPropertiesFromMethods($model)
708710
strpos(get_class($relationObj), 'Many') !== false
709711
)
710712
) {
713+
if ($relationObj instanceof BelongsToMany) {
714+
$pivot = get_class($relationObj->newPivot());
715+
if (!in_array($pivot,[ Pivot::class, MorphPivot::class])) {
716+
$this->setProperty(
717+
$relationObj->getPivotAccessor(),
718+
$this->getClassNameInDestinationFile($model,$pivot),
719+
true,
720+
false
721+
);
722+
}
723+
}
711724
//Collection or array of models (because Collection is Arrayable)
712725
$relatedClass = '\\' . get_class($relationObj->getRelated());
713726
$collectionClass = $this->getCollectionClass($relatedClass);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models;
4+
5+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class ModelWithPivot extends Model
9+
{
10+
public function relationWithCustomPivot()
11+
{
12+
return $this->belongsToMany(ModelwithPivot::class)
13+
->using(CustomPivot::class)
14+
->as('customAccessor');
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;
4+
5+
use Illuminate\Database\Eloquent\Relations\Pivot;
6+
7+
class CustomPivot extends Pivot
8+
{
9+
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot;
4+
5+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
6+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
7+
8+
class Test extends AbstractModelsCommand
9+
{
10+
public function test(): void
11+
{
12+
$command = $this->app->make(ModelsCommand::class);
13+
14+
$tester = $this->runCommand($command, [
15+
'--write' => true,
16+
]);
17+
18+
$this->assertSame(0, $tester->getStatusCode());
19+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
20+
$this->assertMatchesMockedSnapshot();
21+
}
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models;
4+
5+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
/**
9+
*
10+
*
11+
* @property-read CustomPivot $customAccessor
12+
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
13+
* @property-read int|null $relation_with_custom_pivot_count
14+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery()
15+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newQuery()
16+
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot query()
17+
* @mixin \Eloquent
18+
*/
19+
class ModelWithPivot extends Model
20+
{
21+
public function relationWithCustomPivot()
22+
{
23+
return $this->belongsToMany(ModelwithPivot::class)
24+
->using(CustomPivot::class)
25+
->as('customAccessor');
26+
}
27+
}
28+
<?php
29+
30+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots;
31+
32+
use Illuminate\Database\Eloquent\Relations\Pivot;
33+
34+
/**
35+
*
36+
*
37+
* @method static \Illuminate\Database\Eloquent\Builder|CustomPivot newModelQuery()
38+
* @method static \Illuminate\Database\Eloquent\Builder|CustomPivot newQuery()
39+
* @method static \Illuminate\Database\Eloquent\Builder|CustomPivot query()
40+
* @mixin \Eloquent
41+
*/
42+
class CustomPivot extends Pivot
43+
{
44+
45+
}

0 commit comments

Comments
 (0)