【发布时间】:2019-01-25 02:03:20
【问题描述】:
我尝试检查记录是否不存在,然后我将插入,但它不起作用。这是我的代码:
//check if nomor permohonan is exist
$data_pemohon = DB::table('data_pemohon')->select('*')->where('noper', $noper)->get();
if(is_null($data_pemohon)){
return response(null);
}else{
$data_antrian = DB::table('antrian_sp')->select('*')->where('noper', $noper)->first();
if(is_null($data_antrian)){
$nama = DB::table('data_pemohon')->select('nama')->where('noper', $noper)->first();
$status = DB::table('data_pemohon')->select('status_paspor')->where('noper', $noper)->first();
$data = array('tanggal'=>$tanggal, 'jam'=>$jam, 'noper'=>$noper, 'nama'=>$nama->nama, 'status'=>$status->status_paspor);
$add_antrian= DB::table('antrian_sp')->insert($data);
if($add_antrian){
return response($data_pemohon);
}else{
echo "error";
}
}else{
return response(1);
}
}
【问题讨论】:
-
你可以查看这个链接-> stackoverflow.com/questions/27095090/…
-
你得到的结果也请提及。
-
我尝试检查记录是否不存在,然后我将执行插入 只是一条建议,如果您通过代码执行此操作,则必须处理并发。您的
$data_antrian可能在同时执行的代码中有欺骗,您可以使用noper作为主键 以确保只创建一个。另外,我对$data_pemohon很好奇,因为->get()将返回空集合而不是 null,以防它没有结果 cmiiw。 -
如果记录存在?要更新吗?