In this tutorial you will learn about the How to Increment and Decrement Column Value in Laravel and its application with practical example.
In this How to Increment and Decrement Column Value in Laravel tutorial I will show you how to Increment and Decrement Column Value in Laravel. In this tutorial you will learn to Increment and Decrement Column Value in Laravel.
How to Increment and Decrement Column Value in Laravel
In larave framework we have increment() and decrement() method that allows to increment or decrement column values in laravel.
1: Increment a column value Laravel
In this example code you will learn to increment column value using laravel increment() method.
1 2 3 |
Post::find($id)->increment('views'); OR Post::where('id',1)->increment("views"); |
If you want to customize column increment value then you can pass the second argument in the increment() function like below:
1 2 3 |
Post::find($id)->increment('views', 5); OR Post::where('id',1)->increment("views", 5); |
2: Decrement a column value Laravel
In this example code you will learn to increment column value using laravel decrement() method.
1 2 3 |
Post::find($id)->decrement('views'); OR Post::where('id',1)->decrement("views"); |
If you want to customize column decrement value then you can pass the second argument in the decrement() function like below:
1 2 3 |
Post::find($id)->decrement('views', 5); OR Post::where('id',1)->decrement("views", 5); |
3: Increment Or Decrement Without Using Laravel Methods
In this example code you will learn to Increment Or Decrement Without Using Laravel Methods
Increment column value by
1 |
Post::where('id', $id)->update(['views' => DB::raw('views + 1')]); |
Decrement column value by
1 |
Post::where('id', $id)->update(['views' => DB::raw('views - 1')]); |