Table of contents
In my previous post, I shared about Laravel 8, and 9 email verification but the problem is 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 artisan queue:table
Â
Then once done. Run this command also.
php artisan migrate
Â
Then update your .env file and look for QUEUE_CONNECTION then change the value from sync to database.
QUEUE_CONNECTION=database
Â
Step 2: Create a notification named VerifyEmailQueued
Then run this command:
php artisan make:notification VerifyEmailQueued
Â
Once done, navigate the generated class to app/Notifications/ then update the code with the following:
<?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:
/**
* 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 artisan queue:work
Â
Â
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.
Â
Â
I hope it helps. Thank you for reading :)
Read next