In this tutorial you will learn about the Laravel 8 Queue Job Tutorial Example and its application with practical example.
n this Laravel 8 Queue Job Tutorial Example I will show you how to configure, create and use queue jobs in laravel 8 application. In this tutorial you will learn to about creating queue jobs and its application in laravel. In this article I will share example to configure queued jobs for various time consuming tasks in laravel. For example when we want to send mails in bulk it takes enough time to send all mails. Sending mails in bulk result time consuming process and may not guarantee all mails being sent. In laravel we can create a queue job for sending mail in bulk.
Laravel Queue Job
Laravel queue job allows you to process time intensive tasks in background. It results in faster speed and better user experience. With laravel queue jobs you can delay a time-consuming task until a later time. Delaying the time consuming task will speeds up web requests and improve the performance of the applications. For example sending email in bulk is a time-consuming task. We can create a queue job to send email to the user in the background. Laravel queues provide a unified API across various queue backends, such as Amazon SQS, Beanstalk, Redis, or even a relational database.
Laravel 8 Queue Job Tutorial Example
In this step by step tutorial I will demonstrate you with example how to create and use laravel queue jobs in laravel. In this tutorial I will share a simple example to create a queue job with database driver to send email. Please follow the steps given below:
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Configuration SMTP & Database
In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=Add your user name here MAIL_PASSWORD=Add your password here MAIL_ENCRYPTION=tls |
Step 3 – Create Mailable Class
Now, lets create a new laravel mailable class using following artisan command:
1 |
php artisan make:mail SendEmailTest |
Open SendEmailTest.php file and copy bellow code and post on that file.
app/Mail/SendEmailTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php 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 create email view using blade file.
resources/views/emails/test.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html> <head> <title>How to send mail using queue in Laravel 8</title> </head> <body> <center> <h2 style="padding: 23px;background: #b3deb8a1;border-bottom: 6px green solid;"> </h2> </center> <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> </body> </html> |
Step 4: Configuration of Queue
1 |
QUEUE_CONNECTION=database |
After that generate migration and create tables for queue. So run command for queue database tables:
Create Migration:
1 |
php artisan queue:table |
Run Migration:
1 |
php artisan migrate |
Step 5: Create Queue Job for Sending Email
Now we will create a queue job to send emails using following artisan command:
1 |
php artisan make:job SendEmailJob |
Now, open SendEmailJob.php file and put bellow code on that file.
app/Jobs/SendEmailJob.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; 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(); Mail::to($this->details['email'])->send($email); } } |
Step 6: Create Route for Queue Job
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 |
Route::get('email-test', function(){ $details['email'] = 'your_email@gmail.com'; dispatch(new App\Jobs\SendEmailJob($details)); dd('done'); }); |
process laravel queue command.
1 |
php artisan queue:listen |
let see the code changes we made
See configuration.
1 |
php artisan config:clear |
Step 7 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/email-test |