Running Laravel jobs with deleted models

1 min.

If you try to dispatch a job with a model just after it was deleted, you will encounter a small problem:

No query results for model [App\Whatever].

Fortunately, there’s a quick fix for that — just remove SerializesModels trait from the job class. The trait causes the job to retrieve the model instance from database when it is being processed in the queue. This is a prefered behaviour in many cases, but not this one.

Without this trait the whole instance is serialized when dispatching and restored without database involvement when processing in the queue, which is exactly what we want.

This might also serve as optimization technique, saving you some database queries.

P.S. There’s no such problem when processing soft-deleted items, as models are still in the database when processing the queue.

Docs: Class Structure

Do you have comments? Tweet X it at me.

Keep up with me

Consider subscribing. There's also an RSS feed if you're into that.

Wanna reach out? Tweet at me or drop me a line: golb [tod] sadat [ta] olleh.

Est. 2011