mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Add support in morphTo relationship for null values (1547) (#1549)
Co-authored-by: Barry vd. Heuvel <[email protected]>
This commit is contained in:
co-authored by
Barry vd. Heuvel
parent
672acce5ae
commit
f7a3dc8ff2
@@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
- Add type to pivot when using a custom pivot class [#1518 / d3v2a](https://github.com/barryvdh/laravel-ide-helper/pull/1518)
|
- 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)
|
- 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)
|
- Support for Attribute class in attributes [#1567 / stefanScrumble](https://github.com/barryvdh/laravel-ide-helper/pull/1567)
|
||||||
|
|
||||||
|
|||||||
@@ -758,7 +758,8 @@ class ModelsCommand extends Command
|
|||||||
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
|
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
|
||||||
true,
|
true,
|
||||||
null,
|
null,
|
||||||
$comment
|
$comment,
|
||||||
|
$this->isMorphToRelationNullable($relationObj)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//Single model is returned
|
//Single model is returned
|
||||||
@@ -818,6 +819,33 @@ class ModelsCommand extends Command
|
|||||||
return false;
|
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 $name
|
||||||
* @param string|null $type
|
* @param string|null $type
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user