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

david001's avatar

Custom registration with laravel and verify email

Hi, I am not sure how to add verified date at email_verified_at when user click the verify link sent to their email. My code looks like this.

public function store(Request $request)
{
    $user = User::create([
        'name' => request('name'),
        'email' => request('email'),
        'password' => bcrypt(request('password')),
    ]);

    $user->sendEmailVerificationNotification();

    return back();
}
0 likes
6 replies
LaryAI's avatar
Level 58

You can use the built-in Laravel feature to handle email verification.

First, you need to add the email_verified_at column to your users table. You can do this by running the following command:

php artisan make:migration add_email_verified_at_column_to_users_table --table=users

Then, in the up() method of the migration, add the following code:

Schema::table('users', function (Blueprint $table) {
    $table->timestamp('email_verified_at')->nullable();
});

Next, you need to add the MustVerifyEmail trait to your User model.

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    // ...
}

Finally, you need to add the VerifyEmail middleware to your routes.

Route::get('/home', 'HomeController@index')->middleware('verified');

Now, when a user registers, they will receive an email with a verification link. When they click the link, the email_verified_at column will be updated with the current timestamp.

tisuchi's avatar

@david001 You can set your custom method like this logic.

public function verify($id, $hash)
{
    $user = User::findOrFail($id);

    if ($user->email_verified_at !== null) {
        return redirect(route('home'));
    }

    $user->email_verified_at = now();
    $user->save();

    return redirect(route('home'))->with('verified', true);
}
1 like
AddWebContribution's avatar

You can do like this for email verification

public function verifyAccount($token)

{

    $verifyUser = UserVerify::where('token', $token)->first();



    $message = 'Sorry your email cannot be identified.';



    if(!is_null($verifyUser) ){

        $user = $verifyUser->user;

          

        if(!$user->is_email_verified) {

            $verifyUser->user->is_email_verified = 1;

            $verifyUser->user->save();

            $message = "Your e-mail is verified. You can now login.";

        } else {

            $message = "Your e-mail is already verified. You can now login.";

        }

    }



  return redirect()->route('login')->with('message', $message);

}

and if you want to save date in email_verified_at then use $verifyUser->user->email_verified_at = date;

1 like
david001's avatar

hi, thanks for reply, but how do I store the id and token in db that is sent to user email address, so when user clicks the link, I can verify the id and token in the links are valid. Thanks

Please or to participate in this conversation.