Is there any way to get the queued job from the job ID in Laravel? While adding the job to the queue, I store the job ID. Later at some point of time (there is a delay to process the job in the queue), I want to remove the job from the queue. If I can get the job on the queue using the job ID, I can use delete()
method to remove it.
Asked
Active
Viewed 5,577 times
7

Debiprasad
- 5,895
- 16
- 67
- 95
3 Answers
22
I use this code for laravel 5.5 :
use Illuminate\Contracts\Bus\Dispatcher;
$job = ( new JOB_CLASS() )->onQueue('QUEUE_NAME')->delay('DELAY');
$id = app(Dispatcher::class)->dispatch($job);

Armin
- 2,397
- 2
- 21
- 31
-
Is this the only way mate? – Santosh Achari Dec 16 '17 at 11:39
-
2@SantoshAchari Well honestly I couldn't find any other solution and I think this is the easiest way to do it. – Armin Dec 17 '17 at 05:50
-
2021 Lumen v8, worked for me as well. Many thanks, why this isn't in Docs... – Abdul Rehman Jul 12 '21 at 14:51
0
It is a queue so you can not select it, but if you are logging the data also outside the queue you could look in the Queue::before(){} added to AppServiceProvider.php to check the stored id or reference to the jobs as they come off the queue and before processed.
I am also working on this area so if I figure out the code for this, and will post it if I do. As you are getting an event back here in the before() so you have to unwrap it and get the Job out to examine.

tristanbailey
- 4,427
- 1
- 26
- 30
-1
You can use DB::table() for searching the particular job by id, and while dispatching the job it returns the job table's id.
use DB;
class ServiceClass
{
public function deleteJobIfExists($id)
{
$jobTable = 'jobs';
$job = DB::table($jobTable)->find($id);
return $job ? ($job->delete ? 1 : -1) : 0;
}
}