Skip to content

[13.x] Fix wherePivotBetween() being ignored by sync(), detach() and updateExistingPivot() - #61747

Merged
taylorotwell merged 2 commits into
laravel:13.xfrom
sumaiazaman:13.x-fix-where-pivot-between-pivot-operations
Sep 28, 2026
Merged

taylorotwell merged 2 commits into
laravel:13.xfrom
sumaiazaman:13.x-fix-where-pivot-between-pivot-operations

Conversation

@sumaiazaman

Copy link
Copy Markdown
Contributor

wherePivotBetween() only adds its constraint to the relationship's select query. Unlike wherePivot(), wherePivotIn() and wherePivotNull(), it isn't recorded for newPivotQuery(), so pivot table operations run without it.

This can delete data. Given a relation scoped to one year:

public function tags2026()
{
    return $this->belongsToMany(Tag::class)->withPivot('year')->wherePivotBetween('year', [2026, 2026]);
}

$post->tags2026()->sync([4]);

sync() treats the tags from every year as currently attached, so it also detaches the 2024 and 2025 rows. detach() with no arguments removes all of the post's tags, and updateExistingPivot() updates rows outside the range.

This records the wherePivotBetween() arguments the same way wherePivotIn() does and applies them in newPivotQuery(), which also covers orWherePivotBetween() and the NotBetween variants. It's the same kind of issue #61488 fixed for wherePivot() closures.

@taylorotwell
taylorotwell merged commit ac73dc6 into laravel:13.x Sep 28, 2026
61 of 62 checks passed
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