使用队列时不允许序列化 'Closure'
Serialization of 'Closure' is not allowed when using queues
这是我的 RequestMail.php 文件:
protected $phone;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->phone = $request->get('phone');
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('robot@bithub.tech')
->view('request')
->subject('Новая заявка на обмен криптовалют')
->with(['phone' => $this->phone]);
}
我的控制器:
Mail::to('request@domain.com')->queue(new RequestMail($request));
当我尝试对邮件进行排队时,出现以下错误:"Serialization of 'Closure' is not allowed"
编辑 更新了最终代码。
如果您使用队列,则无法序列化包含闭包的对象,这是 PHP 的工作方式。每次将作业推送到队列时,Laravel 将其属性序列化为可写入数据库的字符串,但无法表示匿名函数(例如不属于任何 class 的函数)作为字符串值,因此它们不能被序列化。所以基本上当你将你的 RequestMail
作业推送到队列时,Laravel 会尝试序列化它的属性,但是 $request
是一个包含闭包的对象,所以它不能被序列化。要解决此问题,您必须仅在 RequestMail
class 中存储可序列化的属性:
protected $phone;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->phone = $request->get('phone');
}
public function build()
{
return $this->from('robot@domain.com')
->view('request')
->subject('New request for exchange')
->with(['phone' => $this->phone]);
}
这样做你只保留了你实际需要的 $request
的属性,在本例中是 phone 数字,它是一个字符串,它是完全可序列化的。
编辑
我刚刚意识到这是一个
编辑 2 我已经使用正确的请求参数检索编辑代码以供进一步参考。
这是我的 RequestMail.php 文件:
protected $phone;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->phone = $request->get('phone');
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('robot@bithub.tech')
->view('request')
->subject('Новая заявка на обмен криптовалют')
->with(['phone' => $this->phone]);
}
我的控制器:
Mail::to('request@domain.com')->queue(new RequestMail($request));
当我尝试对邮件进行排队时,出现以下错误:"Serialization of 'Closure' is not allowed"
编辑 更新了最终代码。
如果您使用队列,则无法序列化包含闭包的对象,这是 PHP 的工作方式。每次将作业推送到队列时,Laravel 将其属性序列化为可写入数据库的字符串,但无法表示匿名函数(例如不属于任何 class 的函数)作为字符串值,因此它们不能被序列化。所以基本上当你将你的 RequestMail
作业推送到队列时,Laravel 会尝试序列化它的属性,但是 $request
是一个包含闭包的对象,所以它不能被序列化。要解决此问题,您必须仅在 RequestMail
class 中存储可序列化的属性:
protected $phone;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->phone = $request->get('phone');
}
public function build()
{
return $this->from('robot@domain.com')
->view('request')
->subject('New request for exchange')
->with(['phone' => $this->phone]);
}
这样做你只保留了你实际需要的 $request
的属性,在本例中是 phone 数字,它是一个字符串,它是完全可序列化的。
编辑
我刚刚意识到这是一个
编辑 2 我已经使用正确的请求参数检索编辑代码以供进一步参考。