feat(pivot): add support for multiple pivot types when using the same accessor (#1597)

* feat(pivot): add support for multiple pivot types when using the same accessor

* composer fix-style

* changelog

* resolve namespaces

* Revert "resolve namespaces"

This reverts commit 37559ab089.

* resolve namespaces

---------

Co-authored-by: laravel-ide-helper <[email protected]>
This commit is contained in:
Pieter Willekens
2024-10-28 11:32:19 +01:00
committed by GitHub
co-authored by laravel-ide-helper
parent 4b051e45c7
commit dc70337e48
5 changed files with 104 additions and 3 deletions
+12 -1
View File
@@ -714,14 +714,25 @@ class ModelsCommand extends Command
if ($relationObj instanceof BelongsToMany) {
$pivot = get_class($relationObj->newPivot());
if (!in_array($pivot, [Pivot::class, MorphPivot::class])) {
$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'];
} else {
// pivots are not always set
$pivot .= '|null';
}
$this->setProperty(
$relationObj->getPivotAccessor(),
$this->getClassNameInDestinationFile($model, $pivot),
$pivot,
true,
false
);
}
}
//Collection or array of models (because Collection is Arrayable)
$relatedClass = '\\' . get_class($relationObj->getRelated());
$collectionClass = $this->getCollectionClass($relatedClass);