Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
InvestmentNovel

Latest Programming Tutorial Blog

Laravel 5.7 Email Verification Example Tutorial

/
/
/
90 Views

Laravel 5.7 Email Verification Example Tutorial is the topic, we will discuss today. Laravel is not only a PHP framework, today its know as a new trend of PHP Web Development. Freshly Laravel 5.7 is released with some of the new features, which make development more simple and flexible. Laravel 5.7 provides a new feature called email verification. In this article, we will discuss “Email Verification Laravel 5.7 New Feature”. Lots of website or web applications ask users to verify the email addresses before using the website or applications. Laravel 5.7 makes email verification easy, you don’t want to recreate all the functionality.

Laravel 5.7 Email Verification Example Tutorial

We are going to Configure Laravel Project.

#1: Install Laravel 5.7 Project

composer create-project --prefer-dist laravel/laravel laravel5.7emailverification

#2: Configure SQL Database

Now we can setup database credentials.

//.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelemailverification
DB_USERNAME=root
DB_PASSWORD=

Now, migrate table by the following command.

php artisan migrate

Now, you can see the users table and there is one extra field called email_verified_at.

laravel email verification example

#3: Create Auth Scaffolding

Now move to the terminal and type the following command.

php artisan make:auth

This command has generated one extra view file called verify.blade.php. This file is located in resources/views/auth/verify.blade.php. It is new in Laravel 5.7 as the verification functionality is implemented in this version.

<!--verify.blade.php -->

@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: Add a mustVerify interface in the User model

In a User.php model, you can see extra contract added called  MustVerifyEmail. To use this email verification process, we want to implement this contract.

//User.php

<?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 Route

Go to the routes/web.php file and add the extra parameter inside Auth:: routes().

Auth::routes(['verify' => true]);

This enables the new Verification controller with the route actions.

Also, we want to protect the HomeController route, so let us do that via adding middleware.

//HomeController.php

public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

#6: Configure Email

I am using mailtrap for this example. So log in to the https://mailtrap.io/signin.

Move to the demo inbox and copy the credentials and paste to your .env file.

//.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=Enter Your Username
MAIL_PASSWORD=Enter Your Password
MAIL_ENCRYPTION=tls

#7: Test the email verification functionality

Go to the browser and  type following url on your browser: http://localhost:8000/register

You will see the page like below screenshot.

laravel 5.7 email verification example

Now you can go the mailtrap, and you can see that verification mail has arrived.

laravel email verification tutorial

Also, you can see the database users table and analyze that there is still null in email_verified_at.

laravel 5.7 email verification tutorial

Now, you can click the link that is arrived at your email and your email will be verified and you can see here timestamp will be registered.

laravel email verification

Finally, Our Laravel 5.7 Email Verification Example Tutorial is over. Thanks for taking.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This div height required for enabling the sticky sidebar