How to Use Queue in Laravel 9?

In this article, you will learn how to use a queue in Laravel 9. There are times you will see some processes take time to load like email send, payment gateway, etc. When you send an email for verification or send an invoice then it takes time to load for sending the mail because it is services.

In this example, we will be creating one mail class and create a job class. You can send a test mail using a queue. Follow the below steps:

Step 1: Install Laravel 9

If you have installed the Laravel app, then go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Mail Class with Configuration

Create an email for testing using the Laravel Mail facade. Run the below command:

php artisan make:mail SendEmailTest

Now, you will see a new folder “Mail” in the app directory with the SendEmailTest.php file. Use the below code:


namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendEmailTest extends Mailable
    use Queueable, SerializesModels;
     * Create a new message instance.
     * @return void
    public function __construct()
     * Build the message.
     * @return $this
    public function build()
        return $this->view('emails.test');

Ok, now we require to create an email view using a blade file. So we will create a simple view file and copy the below code on the following path.


<!DOCTYPE html>
    <title>How to send mail using queue in Laravel 9?</title>
<h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;">
    <a href="">Visit Our Website :</a>
<p>Hi, Sir</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<strong>Thank you Sir. :)</strong>

After the view file has been configured and set up for email send, Let’s set the configuration in the .env file:


[email protected]
[email protected]

Step 3: Queue Configuration

Let’s make configuration on the queue driver and then set the queue driver “database”. Define the driver as Redis and the database driver on the “.env” file



Now, generate migration and create tables for the queue. Run the below command for queue database tables:

Generate Migration:

php artisan queue:table

Run Migration

php artisan migrate

Step 4: Create Queue Job

Create a queue job by using the below command, this command creates a queue job file with Queueable. Run the below command:

php artisan make: job SendEmailJob

Now, you have the SendEmailJob.php file in the “Jobs” directory.  Use the below command:


namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\SendEmailTest;
use Mail;
class SendEmailJob implements ShouldQueue
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $details;
     * Create a new job instance.
     * @return void
    public function __construct($details)
        $this->details = $details;
     * Execute the job.
     * @return void
    public function handle()
        $email = new SendEmailTest();

Step 5: Test Queue Job

Now, use and test created queue job, so let’s create simple using the below code for the testing created queue.


Route::get('email-test', function(){
    $details['email'] = '[email protected]';
    dispatch(new App\Jobs\SendEmailJob($details));
Next, run the following command to see the queue process:

php artisan queue: work

You will see the layout as the like below if queue is working:

laravel 9 queue

Run Laravel App

Type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, visit your web browser, type the given URL and view the app output:

laravel 9 queue example

Keep Laravel Queue System Running on Server

We need to keep running the “php artisan work” command on the terminal because then the queue will work. Therefore server, you need to keep running using Supervisor. A supervisor is a process monitor for the Linux operating system, and will automatically restart your queue: work processes if they fail.

Install the Supervisor using the below command:

Install Supervisor

sudo apt-get install supervisor

We need to configure the file on supervisor as below the following path, set project path, user, and output file location as well:


command=php /home/forge/ queue:work --tries=3

Next, start the supervisor using the below commands:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

Now you can check it, from your end.


Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/ on line 5420

Notice: ob_end_flush(): failed to send buffer of zlib output compression (1) in /home/u212091429/domains/ on line 5420