Skip to content
8bityard
8bityard
  • Laravel
  • WordPress
  • PHP
  • Html
  • Bootstrap
  • J query
  • Tools
    • Word Counter
8bityard
8bityard
  • About
  • Contact
  • Portfolio
  • Post
  • Privacy Policy
  • Terms and Conditions
  • Welcome to 8bityard

Laravel email verification example

/ Laravel / By Gaurav Pandey

In this tutorial I will show you a complete (Laravel email verification example), of setup, register user email verification in laravel 5.8 , laravel already provides a simple and very reliable feature – user must verify the email address before logic in laravel 5.8. we will send an activation code on the register email address to verify email.

You just need to make some basic setup with need to use middleware,auth ,routes and mail configuration.

Step 1: Install fresh laravel

Just go to your command prompt and run this installation command in your cmd.

composer create-project --prefer-dist laravel/laravel Emailverification"5.8.*"

Step 2: Setup your Database

Just open your MySQL DB and create a new database name like laravelemailverification then open your env file and setup your DB user name or password

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:429aNb/MLgm3LCVEunUtGw81MzYKAEIT4aDG6K+XxUg=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=yourusername
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Step 3: Make Auth

Just go to your cmd and run command php artisan make:auth

after running this command just run your migration files php artisan migrate , now you can see register or login into the header part.

Laravel-email-verification-example

Step 4: Setup https://mailtrap.io/ account for testing

Laravel-email-verification-example

just open mailtrap.io into your browser and register on it, then you will find out the credentials of Mailtrap just copy and paste it into yours .env file.

Laravel-email-verification-example

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=yourusername
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=null

like this : .env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:429aNb/MLgm3LCVEunUtGw81MzYKAEIT4aDG6K+XxUg=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=yourusername
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Step 4: Setup User.php

Now open User.php file under the app directory and paste the given code

Laravel-email-verification-example
<?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',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Step 4: Setup web.php

Now we will setup our web.php file for caerify email , just copy this code into web.php under the route directory

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

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

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

Step 5: Setup HomeController

this is the last step of email verification, in this step we just use verified middleware in our HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth','verified']);
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

Just run your Project: http://127.0.0.1:8000/

Output: After a successfully registered please check your Mailtrap account for the email verification link

You can also change and edit the design of given blade files these files are: File Directory : resources/views/auth

Read also : How to create and download pdf in Laravel 5.8?

So in this tutorial we learned Laravel email verification example step by step.

Laravel-email-verification-example
Laravel-email-verification-example
Laravel-email-verification-example
Laravel-email-verification-example

Gaurav Pandey

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Post navigation
← Previous Post
Next Post →

Related Posts

Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners

Laravel

How to quickly generate data using faker and tinker ?

Laravel

How to create database seeder in Laravel 5.8 ?

Laravel

The Smart Way To Handle Request Validation In Laravel 5.8 (Laravel form validation)

Laravel

Recent Posts

  • Livewire Multiple Image Remove While UploadingFebruary 8, 2023
  • How to store multiple checkbox values in the database using Livewire?January 31, 2023
  • Accept only Gmail ID while registering in LivewireJanuary 12, 2023
  • Storage link already exists in LaravelDecember 31, 2022
  • Update Status using the toggle switch in LivewireDecember 29, 2022
  • Indian State, City SQL Database in LaravelDecember 23, 2022
  • JSON Encode and Decode in Livewire applicationDecember 21, 2022
  • Sweetalert2 delete confirmation in Livewire ExampleDecember 19, 2022
  • About
  • Portfolio
  • Privacy Policy
  • Terms and Conditions
  • Post
  • Contact

Copyright © 2022 8Bityard