DEV Community

Rakan
Rakan

Posted on

Eloquent Increment and Decrement in Laravel

It turns out that in (Laravel Eloquent) there is an increment and decrement methods built in that you can use to increment or decrement a column value by a given amount.

For example, if you have a balance column in your users table, you can do something like this: $user->increment('balance', 100); to add 100 to the user's balance. You can also do $user->decrement('balance', 100); to subtract 100 from the user's balance.

But make sure that you are not using SIGNED INT for the column, otherwise you will have negative values. You can use UNSIGNED INT instead.

Resources:
- Laravel Eloquent increment()

Top comments (0)