How To Queue the Laravel 8 Email Verification?
Tags: laravel, queue, how, php
Published: 2022-03-25 04:56:14

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-queue-the-laravel-8-email-verification

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)

In my previous [post](https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-8-email-verification), I shared about Laravel 8 email verification but the problem from that when registering an account is too low because we are not using queue on sending email for verification. Now we will implement the queuing on the Laravel 8 email verification so that the user experience is fast.


## Step 1: Setup Laravel Queues

If you don't set up your Laravel queue follow these steps.
 
Run this command to your terminal project:

```php
php artisan queue:table
```

Then once done. Run this command also.

```php
php artisan migrate
```

Then update your `.env` file and look for **QUEUE_CONNECTION** then change the value from **sync** to **database**.

```php
QUEUE_CONNECTION=database
```

## Step 2: Create a notification named VerifyEmailQueued

Then run this command:

```php
php artisan make:notification VerifyEmailQueued
```

Once done, navigate the generated class to **app/Notifications/** then update the code with the following:

```php
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    use Queueable;
}
```

## Step 3: Create a custom method for your User.php model

We will add this method `sendEmailVerificationNotification()` to your User model so that we can customize the notification process. See the following code below:

```php
/**
* Send the queued email verification notification.
*
* @param  string  $token
* @return void
*/
public function sendEmailVerificationNotification()
{
  $this->notify(new VerifyEmailQueued);
}
```

Then that's pretty much it. Now we run the following command to your terminal:

```php
php artisan queue:work
```

![how-to-queue-the-laravel-8-email](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-queue-the-laravel-8-email-verification-J46KowLwyVx980.webp?ctm=1630230570)

Note: We only run **queue:work** manually when developing on local but in production, you must set up the supervisor to handle the queueing.

For more about Laravel queuing just visit their [documentation](https://laravel.com/docs/8.x/queues).

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-queue-the-laravel-8-email-verification if you want to download this code.

Happy coding :)