Skip to content

Eloquent – A better way to increment or decrements an int

Last updated on January 30, 2021

Laravel has in-built increment() and decrement() functions to increase or decrease a value of the column, by 1 or with the given number. Below are the examples:

User::find($user_id)->increment('post_count');

That’s it – it will actually run update column + 1 under the hood.

You can specify a second parameter to this function, the amount of increment. The default is 1, but it can be any other number:

User::find($user_id)->increment('post_count', 50);

The same way, we can use decrement() function:

User::find($user_id)->decrement('post_count', 50);
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments