我想要比较数据库中存在的移动或电子邮件字段,它应该更新行,否则插入行
$ref = Ref::updateOrCreate(
[
'mobile' => $request['mobile'],
'email' => $request['email_address']
],
[
'firstname' => $request['firstname'],
"lastname" => $request['lastname'],
"mobile" => $request['mobile'],
"email" => $request['email_address'],
"mobile_verified" => $req->session()->get('mobile_verified'),
"quiz_data" => $req->session()->get('quiz_data'),
"verification_token" => $req_token
]
);
答案 0 :(得分:0)
这样做
$result= DB::statement("INSERT INTO table_name
(firstname,lastname,mobile,email,mobile_verified,quiz_data,verification_token)
VALUES ('".$request['firstname']."','".$request['lastname']."','".$request['mobile']."','"
.$request['email_address']."','".$req->session()->get('mobile_verified')."','"
.$req->session()->get('quiz_data')."','".$req_token."',)
ON DUPLICATE KEY UPDATE mobile = VALUES(mobile),email = VALUES(email);");
将table_name
替换为您的表名并尝试。
如果之前存在数据,则mobile
和email
将会更新。
答案 1 :(得分:0)
在任一字段上都没有要比较的Laravel函数,您必须手动执行两个查询。
// Find an existing Ref
$ref = Ref::where('mobile', $request['mobile'])
->orWhere('email', $request['email_address'])
->first();
// Test if exists
if (isset($ref))
{
$ref->update([
// Update fields
]);
}
else
{
$ref = Ref::create([
// Create fields
]);
}