Laravel 5.8 Email Verification Tutorial: Are you looking to verify email address setup in laravel 5.8, then you are the right place. In this tutorial, I will example how to set up email verification in laravel 5.8.
Here, You can see a live demo on Email Verification Tutorial
How to use Laravel Email Verification?
Laravel introduce new version 5.8, Then provide email verification after registration in a new feature of laravel 5.8. Laravel provides auth to create a login, registration by default. But if you want to also add email verification then you have to just a few sets and you will be getting set up for email registration.
Laravel 5.8 Email Verification
#1: Create a new Laravel Project
Create a new Laravel project using the below command.
1 |
composer create-project --prefer-dist laravel/laravel email-verification |
#2: Database Configuration
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=email_verify DB_USERNAME=root DB_PASSWORD=your database password |
Now migrate the tables by the following command.
1 |
php artisan migrate |
Now, you can see the users table with one more field called email_verified_at.
The email_verified_at column which is new in Laravel 5.8. So when the user registers and verifies the email, the timestamp will be recorded here.
#3: Create Laravel 5.8 Auth
1 |
php artisan make:auth |
Above Command will generate a new verify.blade.php. It is new in
Laravel 5.8 for Email Verification
functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Verify Your Email Address') }}</div> <div class="card-body"> @if (session('resent')) <div class="alert alert-success" role="alert"> {{ __('A fresh verification link has been sent to your email address.') }} </div> @endif {{ __('Before proceeding, please check your email for a verification link.') }} {{ __('If you did not receive the email') }}, <a href="{{ route('verification.resend') }}">{{ __('click here to request another') }}</a>. </div> </div> </div> </div> </div> @endsection |
#4: Implement mustVerify Interface in the User Model.
In the User.php model, you can see one more contract added called MustVerifyEmail. To use the email verification process, we need to implement this contract.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; } |
#5: Add Email Verification Route.
Go inside routes>>web.php and add the following code.
1 |
Auth::routes(['verify' => true]); |
This enables the new Verification controller with the route actions. You can see the new controller called VerificationController.php file already comes with Laravel 5.8.
1 2 3 4 |
public function __construct() { $this->middleware(['auth', 'verified']); } |
#6: Email Configuration.
I am using mail trap for this example. So log in to the https://mailtrap.io/signin.
You will get the mail credentials after signing up. Paste to your .env file.
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null |
Laravel 5.8 Email Verification Tutorial Example
is over. I hope you have learned from it and will implement for your project. Thank you.