我尝试检查记录是否不存在,然后我将执行插入操作,但是它不起作用。这是我的代码:
//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);
}
}
答案 0 :(得分:0)
看看whereExists:
DB::table('data_pemohon')->whereExists(function($query) {
$query->select(DB::raw(1))
->from('antrian_sp')
->whereRaw('antrian_sp.noper = data_pemohon.noper');
})->get();
答案 1 :(得分:0)
不确定您为什么要使用DB Facade而不是Eloquent:
$data_antrian = App\Antrian_sp::where('noper', $noper)->firstOrFail();
如果您没有用于antrian_sp表的模型,或者只喜欢DB外观,则应该这样做:
if ($data_antrian)