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,
|
||||
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
|
||||
*/
|
||||
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')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fkProp = $reflectionObj->getProperty('foreignKey');
|
||||
$fkProp->setAccessible(true);
|
||||
|
||||
return isset($this->nullableColumns[$fkProp->getValue($relation)]);
|
||||
return isset($this->nullableColumns[$fkProp->getValue($relationObj)]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user