Skip to content

[13.x] Fix custom MorphPivot not touching owners on detach - #61812

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
mrjavadseydi:13.x-fix-morph-pivot-touch-owners
Oct 2, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
mrjavadseydi:13.x-fix-morph-pivot-touch-owners

Conversation

@mrjavadseydi

Copy link
Copy Markdown
Contributor

AsPivot::delete() calls touchOwners() before deleting a pivot without a primary key. MorphPivot::delete() is a copy of that method, but the call was never added, so a custom morph pivot with $touches doesn't touch anything on detach():

class Post extends Model
{
    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable')->using(Taggable::class);
    }
}

class Taggable extends MorphPivot
{
    protected $touches = ['tag'];

    public function tag()
    {
        return $this->belongsTo(Tag::class);
    }
}

$post->tags()->detach($tag); // tags.updated_at doesn't change

The same setup with a regular Pivot updates tags.updated_at. Pivot rows with an id column weren't affected, because that path already goes through Model::delete().

touchOwners() was added to AsPivot::delete() in 2019. When #30229 later copied the deleting/deleted events into MorphPivot::delete(), this call wasn't included. This adds it in the same position. It does nothing when $touches is empty.

@mrjavadseydi
mrjavadseydi marked this pull request as draft October 1, 2026 20:47
@mrjavadseydi
mrjavadseydi marked this pull request as ready for review October 1, 2026 20:57
@taylorotwell
taylorotwell merged commit 7f5157e into laravel:13.x Oct 2, 2026
60 checks passed
@mrjavadseydi
mrjavadseydi deleted the 13.x-fix-morph-pivot-touch-owners branch October 2, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants