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

[9.x] Add "incrementEach" to the Query\Builder #45577

Merged
merged 2 commits into from
Jan 16, 2023

Conversation

imanghafoori1
Copy link
Contributor

@imanghafoori1 imanghafoori1 commented Jan 9, 2023

This is useful when the user wants to atomically increment multiple columns by a single query.
For example, subtract from wallet1 and add the same amount to wallet2 in order to transfer money.

DB::table('users')->where('id', 2)->incrementColumns([
     'wallet_1' => -30,
     'wallet_2' => 30,
], ['updated_at' => now()]);

or keep a proper update of pre-calculated columns:

DB::table('products')->where('id', 2)->incrementColumns([
     'in_store' => 2,
     'in_stock' => 3,
     'total' => 5,
], ['updated_at' => now()]);
  • The logic can be reused in increment to avoid code duplication.
  • The method is named incrementColumns to indicate that it "increments many columns" (not sure if it is the best name).
  • A comprehensive set of integration tests is included. Since the increment now uses incrementColumns the tests also cover it as well, plus some unit tests

@imanghafoori1 imanghafoori1 force-pushed the increment_many branch 10 times, most recently from 92c8c41 to 7273850 Compare January 13, 2023 15:51
@imanghafoori1 imanghafoori1 changed the title [9.x] Add incrementMany to Query\Builder [9.x] Add "incrementColumns" to Query\Builder Jan 13, 2023
@taylorotwell taylorotwell merged commit 33a9413 into laravel:9.x Jan 16, 2023
@taylorotwell
Copy link
Member

Renamed to incrementEach.

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