[12.x] Fix $model->incrementEach() and decrementEach() to scope query to model's primary key#59065
Draft
sumaiazaman wants to merge 1 commit intolaravel:12.xfrom
Draft
Conversation
…odel key When calling $model->incrementEach() or $model->decrementEach() on an existing model instance, the call was forwarded via __call() to the query builder without a WHERE clause scoped to the model's primary key. This caused ALL rows in the table to be updated instead of just the specific model instance — a data corruption bug. This fix adds incrementEach() and decrementEach() methods directly on the Model class (mirroring the existing increment()/decrement() pattern) and registers them in __call() so they are intercepted before being forwarded to the query builder. The new incrementOrDecrementEach() method uses setKeysForSaveQuery() to scope the update to the model's primary key, fires the updating/updated model events, and syncs the model's in-memory attributes after the date. Fixes: laravel#57262
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #57262
When calling
$model->incrementEach()or$model->decrementEach()on an existing Eloquent model instance, the call was forwarded via__call()to the query builder without aWHEREclause scoped to the model's primary key. This caused ALL rows in the table to be updated instead of just the specific model instance — a serious data corruption bug.Root Cause
increment()anddecrement()are listed in__call()and dispatched to the model's ownincrement()anddecrement()are listed in__ctoincrement()anddecrentincrement()decrementEach()were **not** in that list, so they fell thincrement()anddecrement()are listed in__call()and dispatched to the model's ownincremeusincrement()anddncrincrementanddecrementEach()protected methods onModel, mirroring the existingincrement()/decrement()` pattern.incrementOrDecrementEach()helper that usessetKeysForSaveQuery()to scope the update to the model's primary key, firesupdating/updatedmodel events, and syncs the model's in-memory attributes after the update.Tests
Added two unit tests to
DatabaseEloquentModelTest:-------------mentEachOnExistingModelScopesQueryToModelKeyBoth verify that
WHERE id = ?is applied ande model's in-memory attributes are updated correctly.