php - Laravel 8: How to get value increment in multiple columns with a single where statement?
I have a table called Post where there are three separate columns to count post_like, post_dislike, post_react.
Whenever user presses the like or dislike button value in the respective column and also value in post-react
column will increment by one.
I use where statement to increment the value of like/dislike.
Post::where('id', $id)->increment('post_like');
I want to combine the operation of post_react column in the same statement.
Post::where('id', $id)->increment('post_like', ['post_react' => DB::raw( 'post_react + 1' ),]);
The statement fails to execute all together.
**Error on log**
[2020-12-17 07:54:07] local.ERROR: Class 'App\Http\Controllers\DB' not found {"exception":"[object] (Error(code: 0): Class 'App\\Http\\Controllers\\DB' not found at C:\\xampp\\htdocs\\laravelblog\\app\\Http\\Controllers\\PostsController.php:143)
[stacktrace]
Note:
Post::where('id', $id)->increment('post_like', ['post_react' => \DB::raw( 'post_react + 1' ),]);
or
Post::where('id', $id)->increment('post_like')->increment('post_react');
doesn't work either.
Answer
Solution:
From the error message, it seems you forgot to add the DB facade
use Illuminate\Support\Facades\DB;
Source