Email Verification
Introduction
Many web applications require users to verify their email addresses before using the application. Rather than forcing you to re-implement this feature by hand for each application you create, Laravel provides convenient built-in services for sending and verifying email verification requests.
Want to get started fast? Install one of the Laravel application starter kits in a fresh Laravel application. The starter kits will take care of scaffolding your entire authentication system, including email verification support.
Model Preparation
Before getting started, verify that your App\Models\User
model implements the Illuminate\Contracts\Auth\MustVerifyEmail
contract:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Once this interface has been added to your model, newly registered users will automatically be sent an email containing an email verification link. As you can see by examining your application's App\Providers\EventServiceProvider
, Laravel already contains a SendEmailVerificationNotification
listener that is attached to the Illuminate\Auth\Events\Registered
event. This event listener will send the email verification link to the user.
If you are manually implementing registration within your application instead of using a starter kit, you should ensure that you are dispatching the Illuminate\Auth\Events\Registered
event after a user's registration is successful:
use Illuminate\Auth\Events\Registered;
event(new Registered($user));
Database Preparation
Next, your users
table must contain an email_verified_at
column to store the date and time that the user's email address was verified. By default, the users
table migration included with the Laravel framework already includes this column. So, all you need to do is run your database migrations:
php artisan migrate