fix(pivot): only use unique classes in the pivot union (Fixes #1606) (#1607)

* 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:
Pieter Willekens
2024-10-29 15:00:16 +01:00
committed by GitHub
co-authored by laravel-ide-helper
parent 7c2fa01d36
commit 07e3bd8796
3 changed files with 26 additions and 6 deletions
+13 -7
View File
@@ -720,7 +720,7 @@ class ModelsCommand extends Command
$relationReturnType === 'many' || $relationReturnType === 'many' ||
( (
!$relationReturnType && !$relationReturnType &&
strpos(get_class($relationObj), 'Many') !== false str_contains(get_class($relationObj), 'Many')
) )
) { ) {
if ($relationObj instanceof BelongsToMany) { if ($relationObj instanceof BelongsToMany) {
@@ -729,16 +729,22 @@ class ModelsCommand extends Command
$pivot = $this->getClassNameInDestinationFile($model, $pivot); $pivot = $this->getClassNameInDestinationFile($model, $pivot);
if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) { if ($existingPivot = ($this->properties[$relationObj->getPivotAccessor()] ?? null)) {
// If the pivot is already set, we need to append the type to it $existingClasses = explode('|', $existingPivot['type']);
$pivot .= '|' . $existingPivot['type'];
} else { if (!in_array($pivot, $existingClasses)) {
// pivots are not always set array_unshift($existingClasses, $pivot);
$pivot .= '|null';
} }
} else {
// 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( $this->setProperty(
$relationObj->getPivotAccessor(), $relationObj->getPivotAccessor(),
$pivot, $unionType,
true, true,
false false
); );
@@ -33,6 +33,12 @@ class ModelWithPivot extends Model
->using(CustomPivot::class); ->using(CustomPivot::class);
} }
public function relationCustomPivotUsingSameAccessorAndClass()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class);
}
public function relationWithDifferentCustomPivotUsingSameAccessor() public function relationWithDifferentCustomPivotUsingSameAccessor()
{ {
return $this->belongsToMany(ModelwithPivot::class) return $this->belongsToMany(ModelwithPivot::class)
@@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Model;
* @property-read DifferentCustomPivot|CustomPivot|null $pivot * @property-read DifferentCustomPivot|CustomPivot|null $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor * @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
* @property-read int|null $relation_custom_pivot_using_same_accessor_count * @property-read int|null $relation_custom_pivot_using_same_accessor_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessorAndClass
* @property-read int|null $relation_custom_pivot_using_same_accessor_and_class_count
* @property-read CustomPivot|null $customAccessor * @property-read CustomPivot|null $customAccessor
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot * @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
* @property-read int|null $relation_with_custom_pivot_count * @property-read int|null $relation_with_custom_pivot_count
@@ -52,6 +54,12 @@ class ModelWithPivot extends Model
->using(CustomPivot::class); ->using(CustomPivot::class);
} }
public function relationCustomPivotUsingSameAccessorAndClass()
{
return $this->belongsToMany(ModelwithPivot::class)
->using(CustomPivot::class);
}
public function relationWithDifferentCustomPivotUsingSameAccessor() public function relationWithDifferentCustomPivotUsingSameAccessor()
{ {
return $this->belongsToMany(ModelwithPivot::class) return $this->belongsToMany(ModelwithPivot::class)