File tree Expand file tree Collapse file tree 6 files changed +107
-0
lines changed
tests/Console/ModelsCommand/Pivot Expand file tree Collapse file tree 6 files changed +107
-0
lines changed Original file line number Diff line number Diff 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
14152024-03-01, 3.0.0
1516------------------
Original file line number Diff line number Diff line change 3434use Illuminate \Database \Eloquent \Relations \HasOneThrough ;
3535use Illuminate \Database \Eloquent \Relations \MorphMany ;
3636use Illuminate \Database \Eloquent \Relations \MorphOne ;
37+ use Illuminate \Database \Eloquent \Relations \MorphPivot ;
3738use Illuminate \Database \Eloquent \Relations \MorphTo ;
3839use Illuminate \Database \Eloquent \Relations \MorphToMany ;
40+ use Illuminate \Database \Eloquent \Relations \Pivot ;
3941use Illuminate \Database \Eloquent \Relations \Relation ;
4042use Illuminate \Database \Schema \Builder ;
4143use 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 );
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments