I'm trying to check if the user's otp code is correct.
My User_Otp table is structured as below.
id | code | user_id | created_at | updated_at
Sample Data is:
id | code | user_id | created_at | updated_at
19 | 1234 | 27 | 10:12:26 | 10:12:26
20 | 1235 | 27 | 10:12:27 | 10:12:27
21 | 1236 | 27 | 10:12:28 | 10:12:28
I want to only allow user verified when he enter 1236
as otp code because it is the latest code as he tried to resend otp code.
I used below code to achieve this but not working:
$userOtp = UserOtp::where('code', request('code'))->where('user_id', request('user_id'))->latest('created_at')->first();
if ($userOtp) {
// verified
}
CodePudding user response:
The latest
function does only order the result, not filter. This means you will always get a result if the code exists, you could do something like this:
$userOtp = UserOtp::where('user_id', request('user_id'))->latest('created_at')->first();
if ($userOtp->code === request('code')) {
// verified
}