Skip to content
Advertisement

check expire time for each verification code

In the code below i check If there is a record for the user Check if ‍‍‍‍‍‍‍‍exprire_at is smaller than it is now, it will expire and create a new code, otherwise show the user a message. But in both cases a new code is created What is the problem

if (Auth::User()->numberVerifies()->where('type','mobile')->exists()) {

           $expiry = optional(Auth::user()->numberVerifies()->where('type','mobile')->select('expire_at')->first())->expire_at;
           if ($expiry < Carbon::now()) {
                    $code = rand(11111, 99999);
                    numVerify::create([
                        'user_id' => Auth::User()->id,
                        'type' => 'mobile',
                        'code' => $code,
                        'expire_at' => Carbon::now()->addSecond(60)
                    ]);
                    
                    session()->flash('messageMobile', 'code created.');

           }
           elseif ($expiry > Carbon::now()) {
              session()->flash('messageMobile', 'you have a code');
           }

       }

Advertisement

Answer

change

if ($expiry < Carbon::now()) {

to

if (!empty($expiry) && $expiry < Carbon::now()) {

and

} elseif ($expiry > Carbon::now()) {

to

} else {
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement