Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check with latest row in Laravel

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading