Last Updated on July 22, 2023
I was doing development with Laravel Queues and noticed that I am having the following errors.
Class "App\Jobs\DB" not found
Class "App\Jobs\Log" not found
To solve the above error, I added the following code to the top of my Jobs file for it to reference the correct DB and Log class.
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
But even with the additional code, I am still getting the same error.
Actually, even if I change my code inside the Queue Job file, the changes are not reflected in my running Queue Worker.
Solving the Queue Job error
As it turns out, running the Queue Worker with the command php artisan queue:work
caches the existing jobs in memory. So if you change the code of your Job it will not reflect on the current running Jobs.
For development, it’s annoying to keep on stopping and starting the Queue Worker whenever there are changes to the code. What Laravel recommends is using the command php artisan queue:listen
.
php artisan queue:listen
This command will listen to the queue and execute the jobs that are available, but does not cache the jobs in memory.
php artisan queue:listen
is not as efficient php artisan queue:work
but it really helps during the development stage.
For production, use the php artisan queue:work
command. But if there are updates in your code, use this command to restart the queue worker.
php artisan queue:restart
TL;DR
For development, do not use this command.
php artisan queue:work
Use this instead.
php artisan queue:listen
In production, use this command.
php artisan queue:work
Whenever you have changes in your Queue Job’s code, run the following command.
php artisan: queue:restart
Note: I’ve tried this on Laravel 9 and Laravel 10. It’s possible that this is the same solution for earlier versions of Laravel. Let me know in the comments below.