Being one of the most powerful frameworks, Laravel has found a huge user base for itself. It offers many advanced functionalities including the queues that run in the background and help in configuring how the system reacts. They make the work easier by letting you set up the tasks on your priority list. You can use them to delay time-consuming tasks and improve performance.
Furthermore, you can easily configure a queue by following the steps given below:
Configure the queue
Configure the queue using tools like drivers that store the list of tasks for you. Additionally, depending on your work requirements you can also choose among different tools like database, Beanstalkd, Redis, Amazon, IronMQ, etc.
Use the command:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d542e7fbfbfaad29e2ae05_code1l.jpg)
This will create a migration file and create a database table to store the current and failed jobs. It should be visible at the backend in the form of the code below:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d542fde919b68bf3a59ee2_code2.jpg)
There after, run the migrate command again:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d5430851128f2687ad0ecd_code3.jpg)
This shall be visible in the âfailed_jobsâ tables. Then, update the environment file and open the .env file using the below command:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d5431b2361da4b043592e6_code4.jpg)
Check to ensure queue connection:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d543355434b049e16a1815_code5.jpg)
Now that you have successfully created a queue, the next step is to create a job. After this run make: job.â
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54516ba54242d122755a7_code6.jpg)
Here, you can use controller:command to create a skeleton for âapp/Jobsâ in the folder named âMatchSendEmail.php.â
This will result in the below code
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54524f550f2f867cc3830_code7.jpg)
Create an Email Template and Mailable Class
You can easily do this using the make:mailâ.This can be done with the code:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54548f54f2697f2fe5379_code8.jpg)
Testing
Now that you are done with implementing the queue, testing is the next step before you launch it. Add the below code:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d545565a747fe903209b0c_code9.jpg)
â
And then run:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54524f550f2f867cc3830_code7.jpg)
Dispatching
Use the following command for dispatching the jobs with the controller methods.
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d545d7233acbea41e27b47_code11.jpg)
Further, you can also dispatch the jobs conditionally. For this, use theâ dispatch Ifâ and âdispatch unlessâ methods.
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d5460939b184395fcc7d82_code12.jpg)
More over, the jobs can also be dispatched at specific queues using the below code.
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54619233acb5b40e27c69_code13.jpg)
Alternatively, delay the Jobs to a later time by using:
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d5462e1725cea9da635956_code14.jpg)
You may also choose the set tries or timeout properties to the job class, as these values will be used by the queue driver.
Start the employment to process the queue after it is dispatched by using a straight forward command.
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d546356db006e04b1e54f8_code15.jpg)
But the freedom does not end here as you can also specify queue connection, length of time a job can run, the queueâs priorities, process all posts, or a single job, sleep duration between two job executions and do a lot more.
Laravel also lets you easily handle the failed jobs by specifying the number of times it will attempt a job before adding it in the failed job table using the ââtries=5â with the âqueue:workâ in the queue worker. Additionally, you may specify the delay time usingââdelay=5â (number defines the seconds).
![](https://uploads-ssl.webflow.com/61b7282431018c11d04ba98d/61d54645e02791eb43998626_code16.jpg)
Concluding Words
Configuring the Laravel queue is now easy for your work requirements. Although there are many methods to run a queue in Laravel, we have discussed the easiest one above. Go ahead and try this to successfully run the tasks in a hassle-free manner and if you want to know more, reach out to us in the comments below.