'In Laravel 5.3, how to get the Job ID after we dispatch a job to the job queue?
In Laravel 5.3, in the controller, we can dispatch a job to the job queue like this:
$job = (new JobClass())->onQueue('queuename');
dispatch($job);
In the Job class which is using InteractsWithQueue trait, in the handle function, we can get the Job ID like this:
$this->job->getJobId();
But, I would like to get the Job ID in my controller after I use the dispatch($job).
How to get the Job ID in controller?
If there is no such function available, can we extend the dispatch helper function to add this function?
Solution 1:[1]
Its a quite old question. But maybe who came across this post by google.
Laravel is firing an event after a job is queued. Illuminate\Queue\Events\JobQueued
You can add a listener to this event and reach almost all infos incl. The DB id int your event listener.
In your EventServiceProvider attach your listener. In my app it is the JobQueuedListener file
use Illuminate\Queue\Events\JobQueued;
protected $listen = [
JobQueued::class =>[
JobQueuedListener::class
],
In the JobQueuedListener file you can now access the event var.
public function handle(JobQueued $event)
{
$this->event = $event;
$mainListener = $event->job->class ?? '';
if ($mainListener == 'App\Listeners\IpBlockedListener') {
$this->addIdToIpBlockedModel();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike Aron |
