Laravel 9 Queues: How to Use Queue in Laravel 9?
https://www.itsolutionstuff.com/post/laravel-9-queues-how-to-use-queue-in-laravel-9example.html
Here, I will show you laravel 9 queue example. Here you will learn how to use queue in laravel 9. This article goes in detail on laravel 9 queue and job example. I explained simply about laravel 9 queue step by step.
Sometimes, you 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 loads time to send mail because it is services. If you don't want to wait for the user to send an email or other process on loading server-side process then you can use a queue. because it's very fast and visitors will happy to see loading time.
In this example, we will create one mail class and create a job class. you can send test mail using a queue. just follow the below step and make it done this example:
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Step 2: Create Mail Class with Configuration
We are going from scratch and in first step, we will create email for testing using Laravel Mail facade. So let's simple run bellow command.
Now you will have new folder "Mail" in app directory with SendEmailTest.php file. So let's simply copy bellow code and past on that file.
app/Mail/SendEmailTest.php
Ok, now we require to create email view using blade file. So we will create simple view file and copy bellow code om following path.
resources/views/emails/test.blade.php
after configuration of view file, we have to setup for email send, So let' set configuration in .env file:
.env
Read Also: How to Send Mail in Laravel 8 using Mailtrap?
Step 3: Queue Configuration
Now in next step, we will make configuration on queue driver so first of all, we will set queue driver "database". You can set as you want also we will define driver as Redis too. So here define database driver on ".env" file:
.env
After that we need to generate migration and create tables for queue. So let's run bellow command for queue database tables:
Generate Migration:
Run Migration:
Step 4: Create Queue Job
now we will create queue job bey following command, this command will create queue job file with Queueable. So let's run bellow command:
now you have SendEmailJob.php file in "Jobs" directory. So let's see that file and put bellow code on that file.
app/Jobs/SendEmailJob.php
Step 5: Test Queue Job
Now time is use and test created queue job, so let's simple create route with following code for testing created queue.
routes/web.php
Next, you must have to run following command to see queue process, you must have to keep start this command:
You will see layout as like bellow if queue is works:
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
Now, Go to your web browser, type the given URL and view the app output:
Output:
Keep Laravel Queue System Running on Server:
As we know we must need to keep running "php artisan work" command on the terminal because then and then queue will work. so in server, you must have 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.
So let's install Supervisor using bellow command:
Install Supervisor:
Next, we need to configuration file on supervisor as below following path, you can set project path, user and output file location as well:
/etc/supervisor/conf.d/laravel-worker.conf
Next, we will start supervisor with below commands:
Read Also: Laravel 9 Ajax Image Upload Example
Now you can check it, from your end.
I hope it can help you...
Last updated