diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 2368048..1834c79 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -671,6 +671,8 @@ class ModelsCommand extends Command $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); + $customPivots = []; + foreach ( $this->getRelationTypes() as $relation => $impl ) { @@ -716,20 +718,11 @@ class ModelsCommand extends Command 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']; + if(isset($customPivots[$relationObj->getPivotAccessor()])){ + $customPivots[$relationObj->getPivotAccessor()][] = $pivot; } else { - // pivots are not always set - $pivot .= '|null'; + $customPivots[$relationObj->getPivotAccessor()] = [$pivot]; } - - $this->setProperty( - $relationObj->getPivotAccessor(), - $this->getClassNameInDestinationFile($model, $pivot), - true, - false - ); } } @@ -787,6 +780,15 @@ class ModelsCommand extends Command } } } + + foreach($customPivots as $pivotAccessor => $pivots){ + $this->setProperty( + $pivotAccessor, + implode('|', $pivots) . '|null', + true, + false, + ); + } } } } diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index 526abd7..4ef086b 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -11,15 +11,15 @@ use Illuminate\Database\Eloquent\Model; /** * * - * @property-read \DifferentCustomPivot|\CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count - * @property-read \CustomPivot|null $customAccessor + * @property-read DifferentCustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count - * @property-read \DifferentCustomPivot|null $differentCustomAccessor + * @property-read CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot * @property-read int|null $relation_with_different_custom_pivot_count + * @property-read DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery()