gpt4 book ai didi

php - Laravel Queue 如何在作业句柄方法中获取数据

转载 作者:行者123 更新时间:2023-12-04 00:10:06 27 4
gpt4 key购买 nike

我创建了一系列消息,通过 twilio 以短信的形式发送。

我创建了一个 Controller ,将消息与发布请求中收到的数据一起放入队列中。这是我的 Controller 来制作队列:

public function make_queue(Request $request)
{
$data = array (
'phone' => $request->input('phone'),
'message'=> $request->input('message'),
'token'=> $request->input('token')',
'sid'=> $request->input('sid')
);

ProcessMessages::dispatch($data)
->delay(now()->addSeconds(15));

return 'message will be sent';
}

在handle作业中,在handle函数中

public function handle()
{
$token = should_come_from job;
$sid = should_come_from job;
$ids = should_come_from job;
$msg = should_come_from job;
try{
// send message
}
catch (exception $e)
{
handle exception
}

}

我无法弄清楚如何获取句柄函数中的值以实际发送消息....

最佳答案

你需要在你的job handler类(ProcessMessages)中添加一个构造方法,例如:

// namespace and use statements...

class ProcessMessages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $data;

public function __construct(array $data)
{
$this->data = $data;
}

public function handle()
{
$token = $this->data['token'];
// ...
}
}

Once you have written your job class, you may dispatch it using the dispatch method on the job itself. The arguments passed to the dispatch method will be given to the job's constructor. Read about dispatching Jobs.

关于php - Laravel Queue 如何在作业句柄方法中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48836403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com