To catch the job ID in the failed function of a job, you can access it through the $this->job property. Here is an example:
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class YourJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
//
}
public function handle()
{
// Job logic
}
public function failed(\Exception $exception = null)
{
$jobId = $this->job->getJobId();
// Perform actions with the job ID (e.g., sending a notification)
}
}
By using $this->job->getJobId()
, you can retrieve the job ID within the failed
function of your job. This gives you the flexibility to perform specific actions or send notifications related to the failed job using its ID.
lechuhuuha liked this reply
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community