Skip to content

Commit 3073c3a

Browse files
authored
Fix many to many detach without ids broken with custom pivot class (#55490)
1 parent 5b1b8c7 commit 3073c3a

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,21 +470,10 @@ protected function detachUsingCustomClass($ids)
470470
{
471471
$results = 0;
472472

473-
if (! empty($this->pivotWheres) ||
474-
! empty($this->pivotWhereIns) ||
475-
! empty($this->pivotWhereNulls)) {
476-
$records = $this->getCurrentlyAttachedPivotsForIds($ids);
473+
$records = $this->getCurrentlyAttachedPivotsForIds($ids);
477474

478-
foreach ($records as $record) {
479-
$results += $record->delete();
480-
}
481-
} else {
482-
foreach ($this->parseIds($ids) as $id) {
483-
$results += $this->newPivot([
484-
$this->foreignPivotKey => $this->parent->{$this->parentKey},
485-
$this->relatedPivotKey => $id,
486-
], true)->delete();
487-
}
475+
foreach ($records as $record) {
476+
$results += $record->delete();
488477
}
489478

490479
return $results;

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,49 @@ public function testDetachMethod()
336336
$this->assertCount(0, $post->tags);
337337
}
338338

339+
public function testDetachMethodWithCustomPivot()
340+
{
341+
$post = Post::create(['title' => Str::random()]);
342+
343+
$tag = Tag::create(['name' => Str::random()]);
344+
$tag2 = Tag::create(['name' => Str::random()]);
345+
$tag3 = Tag::create(['name' => Str::random()]);
346+
$tag4 = Tag::create(['name' => Str::random()]);
347+
$tag5 = Tag::create(['name' => Str::random()]);
348+
Tag::create(['name' => Str::random()]);
349+
Tag::create(['name' => Str::random()]);
350+
351+
$post->tagsWithCustomPivot()->attach(Tag::all());
352+
353+
$this->assertEquals(Tag::pluck('name'), $post->tags->pluck('name'));
354+
355+
$post->tagsWithCustomPivot()->detach($tag->id);
356+
$post->load('tagsWithCustomPivot');
357+
$this->assertEquals(
358+
Tag::whereNotIn('id', [$tag->id])->pluck('name'),
359+
$post->tagsWithCustomPivot->pluck('name')
360+
);
361+
362+
$post->tagsWithCustomPivot()->detach([$tag2->id, $tag3->id]);
363+
$post->load('tagsWithCustomPivot');
364+
$this->assertEquals(
365+
Tag::whereNotIn('id', [$tag->id, $tag2->id, $tag3->id])->pluck('name'),
366+
$post->tagsWithCustomPivot->pluck('name')
367+
);
368+
369+
$post->tagsWithCustomPivot()->detach(new Collection([$tag4, $tag5]));
370+
$post->load('tagsWithCustomPivot');
371+
$this->assertEquals(
372+
Tag::whereNotIn('id', [$tag->id, $tag2->id, $tag3->id, $tag4->id, $tag5->id])->pluck('name'),
373+
$post->tagsWithCustomPivot->pluck('name')
374+
);
375+
376+
$this->assertCount(2, $post->tagsWithCustomPivot);
377+
$post->tagsWithCustomPivot()->detach();
378+
$post->load('tagsWithCustomPivot');
379+
$this->assertCount(0, $post->tagsWithCustomPivot);
380+
}
381+
339382
public function testFirstMethod()
340383
{
341384
$post = Post::create(['title' => Str::random()]);

0 commit comments

Comments
 (0)