40

Is there a way to increment more than one column in laravel?

Let's say:

DB::table('my_table')
->where('rowID', 1)
->increment('column1', 2)
->increment('column2', 10)
->increment('column3', 13)
->increment('column4', 5);

But this results to:

Call to a member function increment() on integer

I just want to find an efficient way to do this using the given functions from laravel. Thanks. Any suggestions will do.

Vainglory07
  • 5,073
  • 10
  • 43
  • 77

9 Answers9

74

There is no existing function to do this. You have to use update():

DB::table('my_table')
   ->where('rowID', 1)
   ->update([
       'column1' => DB::raw('column1 + 2'),
       'column2' => DB::raw('column2 + 10'),
       'column3' => DB::raw('column3 + 13'),
       'column4' => DB::raw('column4 + 5'),
   ]);
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • hello @lukasgeiter, what if i have something like this : increment('orders.price', 7/$count, ['orders.amount_paid' => 'John']); where $count is a defined variable. How can i proceed? I am trying to add 7 when incremented – chablis-omoba Oct 15 '21 at 18:09
32

Increments and Decrements in Laravel Eloquent Model

Add to cart option is one of the most important functions in e-commerce websites. The tricky part is getting the number of items in the cart to display on the cart icon. The predominant approach to get this done is using the increment and decrement function on Laravel. This also facilitates the addition or removal of a product from your cart. The way to implement this function is ,

$user = User::find(‘517c43667db388101e00000f’);
$user->cart_count++;
// $user->cart_count--; // for decrement the count
$user->save()

An alternate and easier way is,

$user = User::find($article_id);
$user->increment('cart_count');

Also these will work:

$user->increment('cart_count');// increase one count
$user->decrement('cart_count'); // decrease one count
$user->increment('cart_count',10); // increase 10 count
$user->decrement('cart_count',10); // decrease 10 count
Praveen AK
  • 551
  • 7
  • 10
13

Now in laravel 5.7 laravel query builder, increment and decrement, it can be done easily.

Model::where('id', "rowID")->increment('columne1');` 

or you can use DB

DB::table("my_table")->where('id', "rowID")->increment('column1');
Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
Ziaur Rahman
  • 1,148
  • 11
  • 24
10

For future reference in 5.2 it has been made do able by doing the following

You may also specify additional columns to update during the operation:

DB::table('users')->increment('votes', 1, ['name' => 'John']);

Source: https://laravel.com/docs/5.2/queries#updates

Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
8

First off, the result of increment is an integer according to the documentation: http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html

So you would have to do a call for each increment:

DB::table('my_table')
->where('rowID', 1)
->increment('column1', 2);
DB::table('my_table')
->where('rowID', 1)
->increment('column2', 10);
DB::table('my_table')
->where('rowID', 1)
->increment('column3', 13);
DB::table('my_table')
->where('rowID', 1)
->increment('column4', 5);

I'm unable to find any quicker solution, unless you want to solve it with a raw update query command.

Also your example code will probably generate an error as you've closed the statement with ; and continue with a new ->increment call on the next line.

Luceos
  • 6,629
  • 1
  • 35
  • 65
  • 1
    hi, thanks for your fast reponse. but is this efficient? since you call it 4 times? sorry i edited my post about that extra semi-colon – Vainglory07 Apr 23 '15 at 07:15
  • No, see my update; a raw update command is more efficient.. The question is do you consider this as a solution or not. – Luceos Apr 23 '15 at 07:16
  • ya i consider it, but maybe any other solution which i can run it once. – Vainglory07 Apr 23 '15 at 07:17
1
$id=5;
$votes=20;
DB::table('users')
   ->where('id', $id)
   ->update([
       'votes' => DB::raw('votes + '.$votes)
]);
  • could you append your answer with some commentary so that we could better understand your code? – Simas Joneliunas Jun 30 '21 at 02:12
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Al Foиce ѫ Jun 30 '21 at 08:26
1

just use this code

\App\Models\User::find(1)->increment('column1');  //id = 1

or multi recorded

\App\Models\User::where('column1','>','100')->increment('column1');
Waad Mawlood
  • 727
  • 6
  • 10
0

In the latest version of 9.x, you can increment and decrement multiple columns using incrementEach and decrementEach methods:

DB::table('users')->incrementEach([
    'votes' => 5,
    'balance' => 100,
]);

(taken from the documentation)

HTMHell
  • 5,761
  • 5
  • 37
  • 79
0
Model::where('id', "rowID")->increment('columne1');

This works for me

Benson Arafat
  • 452
  • 3
  • 15