Skip to content

[Database] Many-to-Many: detach() method builds incomplete pivot object #46851

@shehi

Description

@shehi

Laravel Version

10.8.0

PHP Version

8.1.17

Database Driver & Version

MariaDb 10.6

Description

When detaching, e.g.:

$ids = [...];
$user->posts()->detach($ids);

https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L478 is triggered, which builds a Pivot object only with pivot-key attributes (e.g. user_id and post_id). The remaining pivot attributes don't get included. This becomes a problem especially when this deleted pivot object is passed to some deleted event. Deleted object not having all its attributes causes issues where those attributes are needed. And that object being already deleted, unfortunately can't be refresh()ed.

At https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php#L513 we can see it potentially could build full pivot object if $this->pivotValues were populated. But the latter is empty.

image

Steps To Reproduce

Description should be enough.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions