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:
1 |
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:
1 |
User::find($user_id)->increment('post_count', 50); |
The same way, we can use decrement()
function:
1 |
User::find($user_id)->decrement('post_count', 50); |
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.