Add support in morphTo relationship for null values (1547) (#1549)

Co-authored-by: Barry vd. Heuvel <[email protected]>
This commit is contained in:
michal
2024-07-12 16:06:56 +02:00
committed by GitHub
co-authored by Barry vd. Heuvel
parent 672acce5ae
commit f7a3dc8ff2
6 changed files with 132 additions and 1 deletions
+29 -1
View File
@@ -758,7 +758,8 @@ class ModelsCommand extends Command
$this->getClassNameInDestinationFile($model, Model::class) . '|\Eloquent',
true,
null,
$comment
$comment,
$this->isMorphToRelationNullable($relationObj)
);
} else {
//Single model is returned
@@ -818,6 +819,33 @@ class ModelsCommand extends Command
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