Skip to content

[12.x] Fix the getCurrentlyAttachedPivots wrong morphClass for morph to many relationships #55721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
}

/**
* Get the pivot models that are currently attached.
* Get the pivot models that are currently attached, filtered by related model keys.
*
* @param mixed $ids
* @return \Illuminate\Support\Collection<int, TPivotModel>
*/
protected function getCurrentlyAttachedPivots()
protected function getCurrentlyAttachedPivotsForIds($ids = null)
{
return parent::getCurrentlyAttachedPivots()->map(function ($record) {
return parent::getCurrentlyAttachedPivotsForIds($ids)->map(function ($record) {
return $record instanceof MorphPivot
? $record->setMorphType($this->morphType)
->setMorphClass($this->morphClass)
Expand Down
59 changes: 59 additions & 0 deletions tests/Integration/Database/EloquentPivotEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ public function testCustomMorphPivotClassDetachAttributes()

$project->equipments()->save($equipment);
$equipment->projects()->sync([]);

$this->assertEquals(
[PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class],
PivotEventsTestModelEquipment::$eventsMorphClasses
);

$this->assertEquals(
['equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type'],
PivotEventsTestModelEquipment::$eventsMorphTypes
);
}
}

Expand Down Expand Up @@ -237,6 +247,55 @@ class PivotEventsTestModelEquipment extends MorphPivot
{
public $table = 'equipmentables';

public static $eventsMorphClasses = [];

public static $eventsMorphTypes = [];

public static function boot()
{
parent::boot();

static::creating(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::created(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::updating(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::updated(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::saving(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::saved(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::deleting(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});

static::deleted(function ($model) {
static::$eventsMorphClasses[] = $model->morphClass;
static::$eventsMorphTypes[] = $model->morphType;
});
}

public function equipment()
{
return $this->belongsTo(PivotEventsTestEquipment::class);
Expand Down