我正在嘗試在我的工作中獲取工作ID。我嘗試$this->job->getJobId()但是它返回一個空字符串。<?phpnamespace 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 }}
2 回答

UYOU
TA貢獻1878條經驗 獲得超4個贊
以下內容將使您獲得工作編號。嘗試復制下面的代碼,并通過簡單的路線進行分發。
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
echo $this->job->getJobId();
}
}
并通過以下途徑對其進行測試。
Route::get('/trigger', function () {
dd(dispatch(new \App\Jobs\TestJob()));
});
在終端中,您現在應該看到以下內容以及給定作業的ID。
如果隊列偵聽器未運行,則可以通過在終端中鍵入以下內容來啟動它
php artisan queue:work redis --tries=3
如果您嘗試將ID返回到控制器/路由,則由于異步/排隊的性質,您將無法通過異步/排隊的作業來執行此操作。
- 2 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消