Skip to content

Commit

Permalink
Add support in morphTo relationship for null values (1547) (#1549)
Browse files Browse the repository at this point in the history
Co-authored-by: Barry vd. Heuvel <[email protected]>
  • Loading branch information
matysekmichal and barryvdh authored Jul 12, 2024
1 parent 672acce commit f7a3dc8
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.

### Added
- Add type to pivot when using a custom pivot class [#1518 / d3v2a](https://github.com/barryvdh/laravel-ide-helper/pull/1518)
- Add support in morphTo relationship for null values [#1547 / matysekmichal](https://github.com/barryvdh/laravel-ide-helper/pull/1547)
- Add support for AsEnumCollection casts [#1557 / Braunson](https://github.com/barryvdh/laravel-ide-helper/pull/1557)
- Support for Attribute class in attributes [#1567 / stefanScrumble](https://github.com/barryvdh/laravel-ide-helper/pull/1567)

Expand Down
30 changes: 29 additions & 1 deletion src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,8 @@ public function getPropertiesFromMethods($model)
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
true,
null,
$comment
$comment,
$this->isMorphToRelationNullable($relationObj)
);
} else {
//Single model is returned
Expand Down Expand Up @@ -818,6 +819,33 @@ protected function isRelationNullable(string $relation, Relation $relationObj):
return false;
}

/**
* Check if the morphTo relation is nullable
*
* @param Relation $relationObj
*
* @return bool
*/
protected function isMorphToRelationNullable(Relation $relationObj): bool
{
$reflectionObj = new ReflectionObject($relationObj);

if (!$reflectionObj->hasProperty('foreignKey')) {
return false;
}

$fkProp = $reflectionObj->getProperty('foreignKey');
$fkProp->setAccessible(true);

foreach (Arr::wrap($fkProp->getValue($relationObj)) as $foreignKey) {
if (isset($this->nullableColumns[$foreignKey])) {
return true;
}
}

return false;
}

/**
* @param string $name
* @param string|null $type
Expand Down
21 changes: 21 additions & 0 deletions tests/Console/ModelsCommand/Morphs/Models/Morphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Morphs extends Model
{
public function relationMorphTo(): MorphTo
{
return $this->morphTo();
}

public function nullableRelationMorphTo(): MorphTo
{
return $this->morphTo();
}
}
24 changes: 24 additions & 0 deletions tests/Console/ModelsCommand/Morphs/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

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

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();
}
}
39 changes: 39 additions & 0 deletions tests/Console/ModelsCommand/Morphs/__snapshots__/Test__test__1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
*
*
* @property string $relation_morph_to_type
* @property int $relation_morph_to_id
* @property string|null $nullable_relation_morph_to_type
* @property int|null $nullable_relation_morph_to_id
* @property-read Model|\Eloquent|null $nullableRelationMorphTo
* @property-read Model|\Eloquent $relationMorphTo
* @method static \Illuminate\Database\Eloquent\Builder|Morphs newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Morphs newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Morphs query()
* @method static \Illuminate\Database\Eloquent\Builder|Morphs whereNullableRelationMorphToId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Morphs whereNullableRelationMorphToType($value)
* @method static \Illuminate\Database\Eloquent\Builder|Morphs whereRelationMorphToId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Morphs whereRelationMorphToType($value)
* @mixin \Eloquent
*/
class Morphs extends Model
{
public function relationMorphTo(): MorphTo
{
return $this->morphTo();
}

public function nullableRelationMorphTo(): MorphTo
{
return $this->morphTo();
}
}
18 changes: 18 additions & 0 deletions tests/Console/ModelsCommand/migrations/____morphs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class MorphsTable extends Migration
{
public function up(): void
{
Schema::create('morphs', function (Blueprint $table) {
$table->morphs('relation_morph_to');
$table->nullableMorphs('nullable_relation_morph_to');
});
}
}

0 comments on commit f7a3dc8

Please sign in to comment.