Skip to content

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

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
imanghafoori1:increment_many
Jan 16, 2023
Merged

[9.x] Add "incrementEach" to the Query\Builder#45577
taylorotwell merged 2 commits into
laravel:9.xfrom
imanghafoori1:increment_many

Conversation

@imanghafoori1

@imanghafoori1 imanghafoori1 commented Jan 9, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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