Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incrementEach and decrementEach dont respect where query when used for eloquent model #49009

Closed
royduin opened this issue Nov 15, 2023 · 5 comments

Comments

@royduin
Copy link
Contributor

royduin commented Nov 15, 2023

Laravel Version

10.32.1

PHP Version

8.1.22

Database Driver & Version

No response

Description

Related: #45577 and #48595, copied the description from that issue:

incrementEach() and decrementEach() dont respect where condition when used for eloquent model.

$user = User::where('shop_id', 1)->first();
$user->decrementEach(['counter' => 1, 'balance' => 100]);

All users in the table are decremented whatever 'shop_id' they have.

Please note that the problem is only if executed via eloquent model , but works correctly if executed via direct query as below

User::where('shop_id', 1)->decrementEach(['counter' => 1, 'balance' => 100]); // works correctly

Steps To Reproduce

See description

@driesvints
Copy link
Member

Yes that's expected and how these methods work. They're not scoped to the single instance of the model.

@federico-boncaldo
Copy link

federico-boncaldo commented Mar 5, 2024

@driesvints Our team had a similar issue with incrementEach and noticed a behaviour difference between the increment method and incrementEach.
If the increment is used on a single instance of the model, the query is applied only to a single row, while incrementEach applies the update to the whole table.
The behaviour of the increment method on the model object seems more intuitive, and it would be great if you could apply the same behaviour to incrementEach and decrementEach.

@CasEbb
Copy link

CasEbb commented Apr 24, 2024

+1

Ran into the same problem. The difference in behaviour between increment and incrementEach is currently confusing and potentially dangerous.

@Roardom
Copy link

Roardom commented May 28, 2024

This was completely unexpected. This made it into prod and I have to restore backups now.

@matthiasPOE
Copy link

@driesvints can you please reconsider opening this there is no way this cant be considered a bug without changing the name of the functions to be more obvious.

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

No branches or pull requests

6 participants