[英]Laravel 8: How to get value increment in multiple columns with a single where statement?
我有一個名為Post的表,其中有三個單獨的列來計算post_like 、 post_dislike 、 post_react 。 每當用戶按下相應列中的喜歡或不喜歡按鈕值時, post-react
列中的值也會增加一。 我使用 where 語句來增加喜歡/不喜歡的值。
Post::where('id', $id)->increment('post_like');
我想將 post_react 列的操作結合在同一個語句中。
Post::where('id', $id)->increment('post_like', ['post_react' => DB::raw( 'post_react + 1' ),]);
該語句無法一起執行。
**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]
筆記:
Post::where('id', $id)->increment('post_like', ['post_react' => \DB::raw( 'post_react + 1' ),]);
或者
Post::where('id', $id)->increment('post_like')->increment('post_react');
也不行。
從錯誤消息中,您似乎忘記添加DB 門面
use Illuminate\Support\Facades\DB;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.