How to increment a value in the database

October 26, 2018

You can just use the ::increment methods to 'plus one' a value. You can also send how many to increment in the 2nd param.

// or to increment by 5

You can also of course use the ::decrement method too.

If you use this as part of the query builder you will face some problems chaining this. It will return an int, not the query builder object. You have to do it like this:

$query = \App\User::where("id",1);
// this will result in an error:
 //although you could do it with just one:
 $num = \App\User::where("id",1)->decrement("profile_view_count",5);