Home » How To Handle Failed Jobs In Laravel

How To Handle Failed Jobs In Laravel

Last updated on May 25, 2021 by

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 the exception 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
failed jobs list in Laravel

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:

  1. Laravel One To One Relationship Tutorial
  2. Best Way to Remove Public from URL in Laravel
  3. How To Print Or Debug Query In Laravel
  4. Specified Key Was Too Long Error In Laravel
  5. AJAX PHP Post Request With Example
  6. How To Use The Laravel Soft Delete
  7. How To Add Laravel Next Prev Pagination
  8. Laravel Remove Package With Example (Correct Way)
  9. Difference Between Factory And Seeders In Laravel
  10. Laravel: Increase Quantity If Product Already Exists In Cart
  11. How To Calculate Age From Birthdate
  12. How to Convert Base64 to Image in PHP
  13. Check If A String Contains A Specific Word In PHP
  14. How To Break Nested Loops In PHP Or Laravel
  15. 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!

 
 

Leave a Comment