Skip to content

Commit ea78060

Browse files
authored
[12.x] Fix the getCurrentlyAttachedPivots wrong morphClass for morph to many relationships (#55721)
* Fix the `getCurrentlyAttachedPivots` wrong morph class for morph to many relationships * Update the `getCurrentlyAttachedPivotsForIds` return type
1 parent 64c440f commit ea78060

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Illuminate/Database/Eloquent/Relations/MorphToMany.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
121121
}
122122

123123
/**
124-
* Get the pivot models that are currently attached.
124+
* Get the pivot models that are currently attached, filtered by related model keys.
125125
*
126+
* @param mixed $ids
126127
* @return \Illuminate\Support\Collection<int, TPivotModel>
127128
*/
128-
protected function getCurrentlyAttachedPivots()
129+
protected function getCurrentlyAttachedPivotsForIds($ids = null)
129130
{
130-
return parent::getCurrentlyAttachedPivots()->map(function ($record) {
131+
return parent::getCurrentlyAttachedPivotsForIds($ids)->map(function ($record) {
131132
return $record instanceof MorphPivot
132133
? $record->setMorphType($this->morphType)
133134
->setMorphClass($this->morphClass)

tests/Integration/Database/EloquentPivotEventsTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ public function testCustomMorphPivotClassDetachAttributes()
179179

180180
$project->equipments()->save($equipment);
181181
$equipment->projects()->sync([]);
182+
183+
$this->assertEquals(
184+
[PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class, PivotEventsTestProject::class],
185+
PivotEventsTestModelEquipment::$eventsMorphClasses
186+
);
187+
188+
$this->assertEquals(
189+
['equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type', 'equipmentable_type'],
190+
PivotEventsTestModelEquipment::$eventsMorphTypes
191+
);
182192
}
183193
}
184194

@@ -237,6 +247,55 @@ class PivotEventsTestModelEquipment extends MorphPivot
237247
{
238248
public $table = 'equipmentables';
239249

250+
public static $eventsMorphClasses = [];
251+
252+
public static $eventsMorphTypes = [];
253+
254+
public static function boot()
255+
{
256+
parent::boot();
257+
258+
static::creating(function ($model) {
259+
static::$eventsMorphClasses[] = $model->morphClass;
260+
static::$eventsMorphTypes[] = $model->morphType;
261+
});
262+
263+
static::created(function ($model) {
264+
static::$eventsMorphClasses[] = $model->morphClass;
265+
static::$eventsMorphTypes[] = $model->morphType;
266+
});
267+
268+
static::updating(function ($model) {
269+
static::$eventsMorphClasses[] = $model->morphClass;
270+
static::$eventsMorphTypes[] = $model->morphType;
271+
});
272+
273+
static::updated(function ($model) {
274+
static::$eventsMorphClasses[] = $model->morphClass;
275+
static::$eventsMorphTypes[] = $model->morphType;
276+
});
277+
278+
static::saving(function ($model) {
279+
static::$eventsMorphClasses[] = $model->morphClass;
280+
static::$eventsMorphTypes[] = $model->morphType;
281+
});
282+
283+
static::saved(function ($model) {
284+
static::$eventsMorphClasses[] = $model->morphClass;
285+
static::$eventsMorphTypes[] = $model->morphType;
286+
});
287+
288+
static::deleting(function ($model) {
289+
static::$eventsMorphClasses[] = $model->morphClass;
290+
static::$eventsMorphTypes[] = $model->morphType;
291+
});
292+
293+
static::deleted(function ($model) {
294+
static::$eventsMorphClasses[] = $model->morphClass;
295+
static::$eventsMorphTypes[] = $model->morphType;
296+
});
297+
}
298+
240299
public function equipment()
241300
{
242301
return $this->belongsTo(PivotEventsTestEquipment::class);

0 commit comments

Comments
 (0)