Laravel 5.8如何获得工作编号? [英] Laravel 5.8 How to get the job Id?

查看:47
本文介绍了Laravel 5.8如何获得工作编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the job ID inside my jobs. I try $this->job->getJobId() but it returns an empty string.

<?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
    }
}

推荐答案

以下内容将使您获得工作ID.尝试复制下面的代码,并通过简单的路由进行分发.

The following will allow you to get the job id. Try to copy the code below and dispatch it with a simple route.

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.

In your terminal, you should now see the following, with the id of your given job.

如果您的队列侦听器没有运行,则可以通过在终端中键入以下内容来启动它

If your queue listener isn't running you can start it by typing the following in the terminal

php artisan queue:work redis --tries=3

如果您尝试将ID返回到控制器/路由,则由于异步/排队的性质,您将无法使用异步/排队的作业来完成此操作.

If you are trying to return the id to your controller/route, you cannot do this with an async/queued job due to the nature of it being async/queued.

这篇关于Laravel 5.8如何获得工作编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆