Sometimes, developers get confused about how to handle failed jobs in Laravel due to lack of knowledge and after some research, they might understand it properly.
In this article, we will see how to handle failed jobs by using different ways. Without wasting a second, let’s just dive into it.
01 How To Check Why Jobs Has Failed In Laravel
If your job has failed then it will be inserted into the `failed_jobs`
database table. If that table doesn’t exist in your database then you can create the table by using the following command.
php artisan queue:failed-table
php artisan migrate
- You can then run your job again and let it fail again
- Now, go to the
failed_jobs
table and check theexception
column, you will get all the information, error, and the reason why your job has failed
02 Retrying Failed Jobs
You can either check failed jobs in failed_jobs
database table or you can run the following Artisan command to get the list of failed jobs.
php artisan queue:failed
After getting the list of failed jobs, you can retry all the jobs in a queue again with the following methods:
2.1 Retry All Failed Jobs
If you want to re-queue all the failed jobs then you can run the following command:
php artisan queue:retry all
After running the above command, it will re-queue all the failed jobs and it will re-insert jobs into the jobs
table. Then you need to run the following command again to run the job.
php artisan queue:listen
2.2 Retry Failed Jobs With ID
We have seen, how to retry all the failed jobs but what if you want to retry the only particular failed jobs? You can do that by running the following command with the job ID
.
php artisan queue:retry 5
In the above command, we are trying to retry the job that has an ID of 5
. Then you again need to run the below command to run the job.
php artisan queue:listen
2.3 Retry Failed Jobs With Multiple IDs
If necessary, you may pass multiple IDs or an ID range (when using numeric IDs) to the command:
php artisan queue:retry 5 6 7 8 9 10
php artisan queue:retry --range=5-10
After retrying multiple failed jobs, you again need to run the below command to run the job:
php artisan queue:listen
Additionally, read our guide:
- Laravel One To One Relationship Tutorial
- Best Way to Remove Public from URL in Laravel
- How To Print Or Debug Query In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- Laravel Remove Package With Example (Correct Way)
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How to Convert Base64 to Image in PHP
- Check If A String Contains A Specific Word In PHP
- How To Break Nested Loops In PHP Or Laravel
- How To Get Latest Records In Laravel
That’s it for now. We hope this article helped you to handle failed jobs in Laravel.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!