我正在使用输入 json 调用登录方法(输入 json 中的字段是 mobile、userType、deviceId 和 deviceType)。我使用此功能所做的是,如果存在具有输入手机号码的用户,则应更新设备详细信息(deviceId 和 deviceType),否则如果不存在此类用户,则应创建一个新用户,并且其所有详细信息都应为插入数据库。在任何一种情况下都应返回当前元组。如果有任何错误,也会返回。
我面临的问题是,当我插入现有用户的手机号码(使用新的 deviceId 和 deviceType)时,它会更新该用户(根据需要)。但是当我再次执行此操作时,它会返回“更新设备详细信息时出错。”。无法弄清楚特定查询如何第一次正确运行,但在那之后就不行了。请帮我。提前致谢。
public function login($data){
$returnData = new \stdClass();
if(empty($data['mobile']) || empty($data['userType'])) {
$returnData->response = "error";
$returnData->message = "Insufficient Input";
return json_encode($returnData);
}
$recd = DB::table('users')
->where('mobile',$data['mobile'])
->where('userType',$data['userType'])
->first();
if(!empty($recd)){//user exists, just return the values
if(isset($data['mobile'])){
$updateDeviceDetails = DB::table('users')
->where('mobile',$data['mobile'])
->update(['deviceId'=>$data['deviceId'], 'deviceType'=>$data['deviceType']]);
} else {
echo "mobile not set.";
}
if($updateDeviceDetails){
$updatedUser = DB::table('users')
->where('id',$recd->id)
->get();
$returnData->response = "success";
$recd->isNewUser = "0";
$returnData->data = $updatedUser;
return json_encode($returnData);
} else {
$returnData->response = "error";
$returnData->message = "Error updating the device details.";
return json_encode($returnData);
}
} else {//user does not exist, create new user
if(empty($data['deviceId']) || empty($data['accessToken']) || empty($data['deviceType'])){
$returnData->response = "error";
$returnData->message = "Insufficient Input";
return json_encode($returnData);
}
$data['created_at'] = $data['updated_at'] = Carbon::now('Asia/Kolkata');
$data['otp'] = mt_rand(100000, 999999);
$data['otpStatus'] = 0;
$data['userType'] = 1;
//insert new user
$newUserId = DB::table('users')->insertGetId($data);
if ($newUserId>0) {
//get the newly inserted user
$newUser = DB::table("users")
->where('id',$newUserId)
->first();
//newUser is the object to be returned
$newUser->isNewUser = "1";
$returnData->response = "success";
$returnData->data = $newUser;
} else {
$returnData->response = "error";
$returnData->message = "Error creating new user.";
}
return json_encode($returnData);
}
}