Home > Mobile >  how to use condition if using DB::table
how to use condition if using DB::table

Time:04-11

I'm trying to update or insert base on condition, but I'm getting error this condition not working. If it notification_status DELETED it was updated table and also it create new record at same time

code

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
    }

CodePudding user response:

use updateOrInsert so you can avoid if condition

 DB::table('notifications')->updateOrInsert(['user_id'=>$request->user_id,'notification_status'=> 'ACTIVE'],['user_name' => $request->user_name])
  • Related