如果使用 DB::table 如何使用条件
how to use condition if using DB::table
我正在尝试根据条件更新或插入,但我收到此条件不起作用的错误。
如果它 notification_status 已删除,它已更新 table 并且它同时创建新记录
代码
use App\Notification;
if(DB::table('notifications')->where('user_id',$request->user_id)->where('notification_status', '=', 'ACTIVE' )){
DB::table('notifications')->update(['user_name' => $request->user_name,]);//if notification_status ACTIVE IT GET UPDATE
}else{
DB::table('notifications')->insert(['user_name' => $request->user_name,]);//if notification_status DELETED CREATE NEW RECORD
}
使用updateOrInsert这样你就可以避免if条件
DB::table('notifications')->updateOrInsert(['user_id'=>$request->user_id,'notification_status'=> 'ACTIVE'],['user_name' => $request->user_name])
我正在尝试根据条件更新或插入,但我收到此条件不起作用的错误。 如果它 notification_status 已删除,它已更新 table 并且它同时创建新记录
代码
use App\Notification;
if(DB::table('notifications')->where('user_id',$request->user_id)->where('notification_status', '=', 'ACTIVE' )){
DB::table('notifications')->update(['user_name' => $request->user_name,]);//if notification_status ACTIVE IT GET UPDATE
}else{
DB::table('notifications')->insert(['user_name' => $request->user_name,]);//if notification_status DELETED CREATE NEW RECORD
}
使用updateOrInsert这样你就可以避免if条件
DB::table('notifications')->updateOrInsert(['user_id'=>$request->user_id,'notification_status'=> 'ACTIVE'],['user_name' => $request->user_name])