我正在尝试在我的工作中获取工作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 回答
![?](http://img1.sycdn.imooc.com/5458643d0001a93c02200220-100-100.jpg)
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 关注
- 121 浏览
添加回答
举报
0/150
提交
取消