Send verification email after registration in laravel 5.7

email verification

The latest version of laravel was just released and there are some major changes in the framework. In this article, we will discuss the email verification after registration in laravel. If you want to know about all the features and upgrades in laravel 5.7, you can read this article.

Laravel is always a hot topic with frequent upgrades and active community among the PHP developers. In the previous version of laravel authentication, there was no activation feature. We have to manually add the activation after registration. But, with this version of laravel, email verification comes by default.

We will discuss the implementation in this article.

Create Project

Since laravel was just released, you may not be able to get the latest version of laravel. If you are unable to install laravel 5.7, you can still install it via dev branch. Run the command:

Authentication Scaffolding

One of the best feature in laravel is that it comes with default authentication. Simply running a single artisan command create all the required routes, controller and view files. To create a authentication scaffolding, run the artisan command:

Model and Migration

In the previous version of laravel also, we get User model and respective migrations file. But, in laravel 5.7, there is a slight change in the migration file as email verification field was added. Email verification is optional. Which means we can implement email verification or not that depends on our needs. When we open the migration file, we can see below code:

Similarly, when we go through our model, we also see a slight change in it. There is a new interface called MustVerifyEmail. By default, User model doesn’t implement this interface. This is a simple interface with three functions.

In our case, we are going to implement it. Thus our User model looks like below:

Routing

We can define if we want to implement email verification or not through routing. For email verification, we must pass verify through route. So, our authentication route looks as below:

Protecting Routes

We can also protect our routes for verified account only by using middleware. To do so, we attach our middleware to respective routes as below:

Controller

To handle the necessary verfication process and other stuff, a new controller called VerificationController has been added. This controller uses VerifiesEmails trait, that does the magic. This trait has 2 functions and below is a screenshot of this trait.

Views

After we run artisan command to create authentication, laravel automatically creates necessary view files under resources/view/auth directory.

Redirect After verfication

When a user is successfully authenticated, our system will automaticallyrecirect to the /home URI. But, we can customize it according to our need. We can redirect to other by defining a redirectTo attribute on the VerificationController

Conclusion

This is it for implementation of email verification in laravel 5.7. This is one of the most requested features in the upgrade from laravel 5.6. Thank you for reading this article. If you have any comment or feedback, feel free to drop a comment below.