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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user