mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-17 17:47:13 +00:00
* fix(pivot): only use unique classes in the pivot union (Fixes #1606) * composer fix-style --------- Co-authored-by: laravel-ide-helper <[email protected]>
This commit is contained in:
co-authored by
laravel-ide-helper
parent
7c2fa01d36
commit
07e3bd8796
@@ -720,7 +720,7 @@ class ModelsCommand extends Command
|
||||
$relationReturnType === 'many' ||
|
||||
(
|
||||
!$relationReturnType &&
|
||||
strpos(get_class($relationObj), 'Many') !== false
|
||||
str_contains(get_class($relationObj), 'Many')
|
||||
)
|
||||
) {
|
||||
if ($relationObj instanceof BelongsToMany) {
|
||||
@@ -729,16 +729,22 @@ class ModelsCommand extends Command
|
||||
$pivot = $this->getClassNameInDestinationFile($model, $pivot);
|
||||
|
||||
if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) {
|
||||
// If the pivot is already set, we need to append the type to it
|
||||
$pivot .= '|' . $existingPivot['type'];
|
||||
$existingClasses = explode('|', $existingPivot['type']);
|
||||
|
||||
if (!in_array($pivot, $existingClasses)) {
|
||||
array_unshift($existingClasses, $pivot);
|
||||
}
|
||||
} else {
|
||||
// pivots are not always set
|
||||
$pivot .= '|null';
|
||||
// No existing pivot property, so we need to add a null type
|
||||
$existingClasses = [$pivot, 'null'];
|
||||
}
|
||||
|
||||
// create a union type of all pivot classes
|
||||
$unionType = implode('|', $existingClasses);
|
||||
|
||||
$this->setProperty(
|
||||
$relationObj->getPivotAccessor(),
|
||||
$pivot,
|
||||
$unionType,
|
||||
true,
|
||||
false
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user