Search code examples
laravellaravel-5eloquentlaravel-5.5laravel-5.6

Laravel 5.6 - Increment multiple same row columns in single statement?


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?


Solution

  • You can use raw query and update. Eg.:

        User::where('id',$userId)->update([
            'column1' => DB::raw('column1 + 1'),
            'column2' => DB::raw('column2 + 1'),
        ]);