Laravel 5.8如何获得工作编号?

我正在尝试在我的工作中获取工作ID。我尝试$this->job->getJobId()但是它返回一个空字符串。


<?php


namespace App\Jobs\Notifications;


use Illuminate\Bus\Queueable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Support\Facades\Auth;


class SendNotification implements ShouldQueue

{

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    public function __construct($notification, $fireShutdown)

    {

        $this->notification = $notification;

        $this->fireShutdown = $fireShutdown;

    }


    public function handle()

    {

        dd($this->job->getJobId());


       // Some Code

    }

}


郎朗坤
浏览 122回答 2
2回答

UYOU

以下内容将使您获得工作编号。尝试复制下面的代码,并通过简单的路线进行分发。class TestJob implements ShouldQueue{&nbsp; &nbsp; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Execute the job.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function handle()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo $this->job->getJobId();&nbsp; &nbsp; }}并通过以下途径对其进行测试。Route::get('/trigger', function () {&nbsp; &nbsp; dd(dispatch(new \App\Jobs\TestJob()));});在终端中,您现在应该看到以下内容以及给定作业的ID。如果队列侦听器未运行,则可以通过在终端中键入以下内容来启动它php&nbsp;artisan&nbsp;queue:work&nbsp;redis&nbsp;--tries=3如果您尝试将ID返回到控制器/路由,则由于异步/排队的性质,您将无法通过异步/排队的作业来执行此操作。
打开App,查看更多内容
随时随地看视频慕课网APP