Skip to content
Advertisement

how to excute duplicate key update in laravel

I want to duplicate key update it, but it doesn’t work.

How to Duplicate key update in laravel?

insert into tbl_notify 
(send_no, location_id, location_nm, msg, create_dt)
values
(value1, value2, value3, value4, value5) on duplicate key update
location_nm = value3, msg = value4 

in laravel ..

$re = DB::table('tbl_notify')->updateorInsert(
  ['send_no' => $data->md101_sn , 'location_id' => $data->location_id],
  ['location_nm' => $data->location_name, 'msg' => $data->msg, 'send_dt' => $data->create_date]);

enter image description here

Advertisement

Answer

Follow this method to update your information in case key already exist. You must need to match unique data and update the rest.

$re = DB::table('tbl_notify')->where([['send_no' => $data->md101_sn], ['location_id' => $data->location_id]])->update(['location_nm' => $data->location_name, 'msg' => $data->msg]); 
          return response()->json([
                'success' => true,
                'message' => 'Already User Exists Location Updated', 
            ], Response::HTTP_OK);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement