Mohammed Manssour

The Forgotten Laravel Table

LaravelpackageOpen Source

Every Laravel app has a table most people never look at. It is called failed_jobs.

It sits quietly in your database. You forget it is there. Until something breaks.

Let me explain what it is, why it matters, and how to make it easy to work with.

What is the failed_jobs table?

You push jobs onto a queue and move on. Most of them run fine. But some fail. The mail server is down. The image is broken. The card is declined.

When a job fails, Laravel does not throw it away. It saves it in the failed_jobs table.

Each row holds:

  • The name of the job
  • When it failed
  • The full error
  • All the data the job needed

So nothing is lost. You can look at it later. You can even run it again.

Why do we need it?

Because failures happen. And you need to know about them.

Without this table, a failed job would just vanish. The email never sent. And you would never know.

With it, you can:

  • See what broke
  • Read the error
  • Fix the cause
  • Try the job again

It is your safety net.

The problem

The table is helpful. But it is not easy to work with.

Here is why.

The job name and its data are stored as one big block of text. To find all failed emails, you have to dig inside that text. It is slow and clumsy.

The error messages are long. Searching them is hard.

And the table never cleans itself. Every failure stays forever. After a year, it can hold millions of rows. Your database gets big and slow.

Most teams only notice when it is too late.

A package that helps

I built a small package to fix all of this. It is called failed-jobs-model.

It does three things.

1. It makes the table easy to read

You get a simple model. Find a job by name in one line:

FailedJob::findByDisplayName('App\Jobs\SendEmail');

Search the errors:

FailedJob::whereExceptionContains('timed out')->get();

No digging through text. And it is fast, because the package adds a hidden index for you.

2. It gives you simple commands

See which jobs fail the most:

php artisan failed-jobs:stats

See how big the table has grown:

php artisan failed-jobs:size

3. It cleans up after itself

Old failures get removed on their own. You pick how long to keep them. The package does the rest.

You can also clean up by hand any time:

php artisan failed-jobs:prune --hours=24

Get started

Install it with one line:

composer require mohammedmanssour/failed-jobs-model

Then run your migrations. That is it.

Link: https://packagist.org/packages/mohammedmanssour/failed-jobs-model

The end

The failed_jobs table is easy to forget. But it is one of the most useful tables you have.

Give it a little care. It will save you on a bad day.

Crafted with love by Mohammed Manssour