Description
Laravel Version
12.9.0
PHP Version
8.3.20
Database Driver & Version
sqlite
Description
As of Laravel 12.9.0 the functionality to detach all pivot records on a ManyToMany relation by calling detach
without any ids is broken when using a custom pivot model.
The commit 99e1ceb seems to be the problem, where changes have been made to fix pivot model events being fired.
What would happen before is that the && ! empty($ids)
call below would cause the else clause to be performed, where a simple delete
of all pivot rows would be performed:
But in 12.9.0 this has been changed to only check $this->using
, and as we are using a custom pivot model, we got to the detachUsingCustomClass
method.
Inside that method there is no check if $ids
is empty, so we end up in the foreach ($this->parse($ids) as $id)
line, but $ids
is empty, so nothing will be deleted.
Steps To Reproduce
- Create a new Laravel project
- Create a
Role
model and migration - Create a
RoleUser
pivot model. - Setup the
BelongsToMany
relation without->using(RoleUser::class)
- Write a simple test that creates a
User
andRole
and attach them. - Now call
$user->roles()->detach()
and you'll see the pivot record will be deleted. - Now add
->using(RoleUser::class)
to theBelongsToMany
relation. - Run the test again, and you'll see the pivot record is not deleted.