1

Using eloquent model User, how can we increment 2 columns on the same row by 1 (in a single statement)?

This single statement works but only for incrementing a single column:

User::where('id',$userId)->increment('column1');

These following two attempts did not work:

User::where('id',$userId)->increment('column1')->increment('column2');

User::where('id',$userId)->increment(['column1','column2']);

Any ideas how to solve this in a single statement?

Wonka
  • 8,244
  • 21
  • 73
  • 121

1 Answers1

6

You can use raw query and update. Eg.:

    User::where('id',$userId)->update([
        'column1' => DB::raw('column1 + 1'),
        'column2' => DB::raw('column2 + 1'),
    ]);
Rafal Migda
  • 674
  • 3
  • 9