mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Make hasOne / hasOneThrough and morphOne nullable. (#864)
This commit is contained in:
@@ -563,7 +563,7 @@ class ModelsCommand extends Command
|
|||||||
true,
|
true,
|
||||||
null,
|
null,
|
||||||
'',
|
'',
|
||||||
$this->isRelationForeignKeyNullable($relationObj)
|
$this->isRelationNullable($relation, $relationObj)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -575,22 +575,32 @@ class ModelsCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the foreign key of the relation is nullable
|
* Check if the relation is nullable
|
||||||
*
|
*
|
||||||
* @param Relation $relation
|
* @param string $relation
|
||||||
|
* @param Relation $relationObj
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isRelationForeignKeyNullable(Relation $relation)
|
private function isRelationNullable(string $relation, Relation $relationObj): bool
|
||||||
{
|
{
|
||||||
$reflectionObj = new \ReflectionObject($relation);
|
$reflectionObj = new \ReflectionObject($relationObj);
|
||||||
|
|
||||||
|
if (in_array($relation, ['hasOne', 'hasOneThrough', 'morphOne'], true)) {
|
||||||
|
$defaultProp = $reflectionObj->getProperty('withDefault');
|
||||||
|
$defaultProp->setAccessible(true);
|
||||||
|
|
||||||
|
return !$defaultProp->getValue($relationObj);
|
||||||
|
}
|
||||||
|
|
||||||
if (!$reflectionObj->hasProperty('foreignKey')) {
|
if (!$reflectionObj->hasProperty('foreignKey')) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fkProp = $reflectionObj->getProperty('foreignKey');
|
$fkProp = $reflectionObj->getProperty('foreignKey');
|
||||||
$fkProp->setAccessible(true);
|
$fkProp->setAccessible(true);
|
||||||
|
|
||||||
return isset($this->nullableColumns[$fkProp->getValue($relation)]);
|
return isset($this->nullableColumns[$fkProp->getValue($relationObj)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ class Simple extends Model
|
|||||||
return $this->hasOne(Simple::class);
|
return $this->hasOne(Simple::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function relationHasOneWithDefault(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Simple::class)->withDefault();
|
||||||
|
}
|
||||||
|
|
||||||
public function relationBelongsTo(): BelongsTo
|
public function relationBelongsTo(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Simple::class);
|
return $this->belongsTo(Simple::class);
|
||||||
|
|||||||
@@ -81,10 +81,11 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|||||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToSameNameAsColumn
|
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToSameNameAsColumn
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationHasMany
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationHasMany
|
||||||
* @property-read int|null $relation_has_many_count
|
* @property-read int|null $relation_has_many_count
|
||||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple $relationHasOne
|
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple|null $relationHasOne
|
||||||
|
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple $relationHasOneWithDefault
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationMorphMany
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationMorphMany
|
||||||
* @property-read int|null $relation_morph_many_count
|
* @property-read int|null $relation_morph_many_count
|
||||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple $relationMorphOne
|
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple|null $relationMorphOne
|
||||||
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $relationMorphTo
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $relationMorphTo
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationMorphedByMany
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationMorphedByMany
|
||||||
* @property-read int|null $relation_morphed_by_many_count
|
* @property-read int|null $relation_morphed_by_many_count
|
||||||
@@ -107,6 +108,11 @@ class Simple extends Model
|
|||||||
return $this->hasOne(Simple::class);
|
return $this->hasOne(Simple::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function relationHasOneWithDefault(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Simple::class)->withDefault();
|
||||||
|
}
|
||||||
|
|
||||||
public function relationBelongsTo(): BelongsTo
|
public function relationBelongsTo(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Simple::class);
|
return $this->belongsTo(Simple::class);
|
||||||
|
|||||||
Reference in New Issue
Block a user