DEV Community

Rakan
Rakan

Posted on

2

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay