mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57: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 === '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'];
|
|
||||||
|
if (!in_array($pivot, $existingClasses)) {
|
||||||
|
array_unshift($existingClasses, $pivot);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// pivots are not always set
|
// No existing pivot property, so we need to add a null type
|
||||||
$pivot .= '|null';
|
$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)
|
||||||
|
|||||||
Reference in New Issue
Block a user