Skip to content
New issue

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

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

Already on GitHub? # to your account

Add pivot class property #1518

Merged
merged 5 commits into from
Mar 11, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
### Changed

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

2024-03-01, 3.0.0
------------------
Expand Down
13 changes: 13 additions & 0 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -708,6 +710,17 @@ public function getPropertiesFromMethods($model)
strpos(get_class($relationObj), 'Many') !== false
)
) {
if ($relationObj instanceof BelongsToMany) {
$pivot = get_class($relationObj->newPivot());
if (!in_array($pivot,[ Pivot::class, MorphPivot::class])) {
$this->setProperty(
$relationObj->getPivotAccessor(),
$this->getClassNameInDestinationFile($model,$pivot),
true,
false
);
}
}
//Collection or array of models (because Collection is Arrayable)
$relatedClass = '\\' . get_class($relationObj->getRelated());
$collectionClass = $this->getCollectionClass($relatedClass);
Expand Down
16 changes: 16 additions & 0 deletions tests/Console/ModelsCommand/Pivot/Models/ModelWithPivot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
use Illuminate\Database\Eloquent\Model;

class ModelWithPivot extends Model
{
public function relationWithCustomPivot()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class)
->as('customAccessor');
}
}
10 changes: 10 additions & 0 deletions tests/Console/ModelsCommand/Pivot/Models/Pivots/CustomPivot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

use Illuminate\Database\Eloquent\Relations\Pivot;

class CustomPivot extends Pivot
{

}
22 changes: 22 additions & 0 deletions tests/Console/ModelsCommand/Pivot/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;

class Test extends AbstractModelsCommand
{
public function test(): void
{
$command = $this->app->make(ModelsCommand::class);

$tester = $this->runCommand($command, [
'--write' => true,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
$this->assertMatchesMockedSnapshot();
}
}
45 changes: 45 additions & 0 deletions tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

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

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Pivot\Models\Pivots\CustomPivot;
use Illuminate\Database\Eloquent\Model;

/**
*
*
* @property-read CustomPivot $customAccessor
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
* @property-read int|null $relation_with_custom_pivot_count
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot query()
* @mixin \Eloquent
*/
class ModelWithPivot extends Model
{
public function relationWithCustomPivot()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class)
->as('customAccessor');
}
}
<?php

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

use Illuminate\Database\Eloquent\Relations\Pivot;

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

}