Skip to content

Apply wherePivotNull to pivot queries - #41

Draft
tlsqr wants to merge 3 commits into
Weebly:masterfrom
tlsqr:pivot-where-constraints
Draft

tlsqr wants to merge 3 commits into
Weebly:masterfrom
tlsqr:pivot-where-constraints

Conversation

@tlsqr

@tlsqr tlsqr commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

BelongsToMany::newPivotQuery() only applied wherePivot() and wherePivotIn() constraints. Laravel's own newPivotQuery() also applies wherePivotNull(). Because the override skipped it, detach(), sync(), toggle() and updateExistingPivot() ignored wherePivotNull() on relations without a custom pivot class. For example, $model->tags()->wherePivotNull('extra')->detach() deleted every pivot row of the parent.

Changes

  • newPivotQuery() applies pivotWhereNulls after the wherePivotIn() constraints, as Laravel does.

Behaviour change on Laravel 10-12

wherePivotNull() now narrows detach(), sync(), toggle() and updateExistingPivot(), as it does in Laravel itself. Code that relied on those calls touching every pivot row will see fewer rows changed.

Testing

2 new tests in PivotTest. Both fail without the src change. They pass locally on PHP 8.3 against SQLite on Laravel 10.50.3, 11.57.0, 12.70.0 and 13.36.0, with no skips.

The Laravel 10 and 11 CI legs fail at composer update on this branch, as they do on master. #40 fixes that.

Merging after #40

#40 applies wherePivotBetween() in newPivotQuery() on Laravel 13. When this branch is rebased onto it:

  • The wherePivotNull() loop goes after the wherePivotBetween() loop, which is Laravel's order.
  • A test for that order comes back. It was dropped here because it needs both loops.
  • Add Laravel 13 support #40 also adds the test helpers this branch adds, so the duplicates go.

Split out of #40, the Laravel 13 support PR, since it changes behaviour on Laravel 10-12.

🤖 Generated with Claude Code

newPivotQuery() only applied wherePivot() and wherePivotIn(), so
detach(), sync(), toggle() and updateExistingPivot() ignored
wherePivotNull() constraints and touched every pivot row of the
parent. Laravel 13 also applies wherePivotBetween() there.

Apply both, in Laravel's order. On Laravel 10-12 this narrows those
operations when wherePivotNull() is used, matching Laravel itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tlsqr tlsqr mentioned this pull request Oct 15, 2026
tlsqr and others added 2 commits October 15, 2026 11:59
Matches the review feedback on Weebly#40: the comments restate what the tests
already cover. Rename attachTwoPlain() and second_models_plain() to say
what they lack: a custom pivot class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Applying wherePivotBetween() in pivot queries only matters on Laravel
13, so it moves to Weebly#40 with its tests. This PR keeps the
wherePivotNull() fix for Laravel 10-12. The constraint order test needs
both loops, so it returns when this branch is rebased onto Weebly#40.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tlsqr tlsqr changed the title Apply wherePivotNull and wherePivotBetween to pivot queries Apply wherePivotNull to pivot queries Oct 15, 2026
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.

1 participant