2
votes

I have a Laravel job that I dispatch to read customer data from a file. The job has a logger model dependency, and this logger tracks certain stats then writes to a log db table when the job completes. When I dispatch this job immediately it works fine, but if I queue the exact same job with the exact same parameters and then run it from the queue it fails with the error:

[Illuminate\Database\Eloquent\ModelNotFoundException]               
  No query results for model [app\Models\MyModel].

I think this is due to the serialize/unserialize nature inherent in the Laravel queue system which breaks Eloquent, which tries the findOrFail() method when I am never actually trying to find something in the db. I am merely creating a new log from a blank model. Whatever the case, it is clear that the same Laravel job, when not queued, runs fine -- but it fails when queued. I am at a loss and wondering if there is a way to work around this issue.

Thanks.

2
can you add all your code? the dispatch function calling the queue job and the Job itself?benjah

2 Answers

2
votes

I worked around it by instantiating and then saving the model object before passing it into the job's constructor, so it at least has a primary key set. This eliminates the findOrFail() method. A little hacky but it's the best I could come up with.

-2
votes

Make an error handler (put it in your routes)

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $e)
{
    return Response::make('Not found',404);
});