Skip to content

ManyToMany relation detach without ids broken after 12.9.0 upgrade #55428

@MatasElectronics

Description

@MatasElectronics

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:

Image

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.

Image

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.

Image

Steps To Reproduce

  1. Create a new Laravel project
  2. Create a Role model and migration
  3. Create a RoleUser pivot model.
  4. Setup the BelongsToMany relation without ->using(RoleUser::class)
  5. Write a simple test that creates a User and Role and attach them.
  6. Now call $user->roles()->detach() and you'll see the pivot record will be deleted.
  7. Now add ->using(RoleUser::class) to the BelongsToMany relation.
  8. Run the test again, and you'll see the pivot record is not deleted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions