php - 通过 API 将登录 OTP 插入到我的数据库的“OTP”列中以获取所请求的电子邮件
问题描述
我的 API 结果为 OTP,但它没有被插入到数据库中。如果作为输入提供的电子邮件是数据库中已经存在的有效电子邮件,我将尝试生成一个 OTP 并将其保存到表中的OTP
列中users
。它更像是一个登录 OTP API,其中 otp 只是被插入到数据库中,而不是发送到手机号码或电子邮件 ID。有人猜我哪里错了吗?我只是 REST API 的初学者。所以,需要帮助解决这个问题。
public function sendotp(Request $request){
$validator = Validator::make
($request->all(),
[
'email' => 'required|email',
]
);
if ($validator->fails()) {
return response()->json(
[$validator->errors()],
422
);
}
$email_user = User::where('email',$request->email)->first();
if($email_user){
$otp = rand(1000,9999);
$user = User::where('email','=',$request->email)->update(['OTP' => $otp]);
$user->save();
return response()->json($otp);
}
else{
return response()->json(['message'=>'No users exists with this email'], 404);
}
}
解决方案
您在更新之前返回响应所以更改如下
$user = User::where('email',$request->email)->first();
if($user){
$otp = rand(1000,9999);
$user->OTP=$otp;
$user->save();
return response()->json($otp);
} else{
return response()->json(['message'=>'No users exists with this email'], 404);
}
还要确保您是否正在更新,然后在模型中使列OTP
可填充 User
推荐阅读
- python - 我无法从树节点中删除数据,当我再次查看它时它才显示
- here-api - java.lang.RuntimeException:无法创建地图:无法读取配置文件:
- asp.net - 如何在模态 div 中调用局部视图,并将对象参数传递给它?
- ios - 如何检测图表标记上的点击手势?
- xamarin - Xamarin 表单样式类绑定
- python - 在数据库中创建新表时如何更新 models.py?
- android - 用户选择帐户后,Google Drive API 不会让用户保持登录状态
- python - Python Google Drive API Listing Files 只列出了 10 个文件
- spring - 审核死信队列消息是否使用 Apache camel
- javascript - 如何使脚本加载和 es6 模块加载一起工作?