Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohamedyasser27's avatar

Handle OTP Expiration Time

Hello guys , i'm creating user verification using an SMS sent his/her phone and this is my database table for it:

Schema::create('otps', function (Blueprint $table) {
            $table->id();
            $table->string('code', 6);
            $table->boolean('is_verified')->default(false);
            $table->foreignId('user_id')->nullable()->constrained('users')->cascadeOnDelete();
            $table->timestampTz('expires_at');
            $table->softDeletes();
            $table->timestampsTz();

and i am thinking of replacing the expires_at column with a separate config file that holds the expiration time like this:

<?php

return [
    'expiration_time' => env('OTP_EXPIRATION_TIME', 15),
];

which approach is better?

0 likes
4 replies
Snapey's avatar

you say which is better, but you only showed one approach. You cannot remove expiration time from the table because this needs to hold the timestamp of when their 2fa code will no longer be valid

mohamedyasser27's avatar

@Snapey thank you for your response and sorry if i wasn't clear enough, what i meant is that i could use the config file alongside the created_at property like this for example

Carbon::parse(Otp::where('user_id', $id)->created_at)->diffInMinutes(Carbon::now())

when i look at it now, it feels like too much of a hassle, but i am trying to learn when to use each part of laravel, like when should i create a config file, middleware,etc

Snapey's avatar

Either way you should avoid magic numbers in your code and move the 15 to config

1 like

Please or to participate in this conversation.