Posts Learn Snippets Categories Tags About
/

How to Increment and Decrement Integer Column in Laravel

Learn how to easily increment and decrement your integer column and save your time

Created on Jul 21, 2021

5 views

To increment and decrement an integer column value in Laravel, you can make use of the built-in method available to every Laravel Eloquent or through the DB query builder.

DB Query Builder
Using the DB query builder it would look like below.
DB::table('posts')->increment('count');

DB::table('posts')->increment('count', 10);

DB::table('posts')->decrement('count');

DB::table('posts')->decrement('count', 10);

Laravel Eloquent
The other way is by using the eloquent model.
Post::find(1)->increment('count');

Post::find(1)->increment('count', 10);

Post::find(1)->decrement('count');

Post::find(1)->decrement('count', 10);

Created by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.