不允许 Laravel 工作序列化“关闭”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49157861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Jobs Serialization of 'Closure' is not allowed
提问by Stan Barrows
I would like to send Data to a NewsletterStore Job. But it's failing with the following error. Any suggestions?
我想将数据发送到 NewsletterStore 作业。但它因以下错误而失败。有什么建议?
I also tried to remove the SerializesModels Models trait. Without any success.
我还尝试删除 SerializesModels 模型特征。没有任何成功。
Error
错误
Exception
Serialization of 'Closure' is not allowed
Controller
控制器
public function store(StoreNewsletterRequest $request)
{
StoreNewsletterJob::dispatch($request);
return view('backend.dashboard.index');
}
Job
工作
protected $request;
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if(!Newsletter::isSubscribed($this->request->email))
{
Newsletter::subscribe($this->request->email, [
config('newsletter.list_fields.firstname') => $this->request->firstname,
config('newsletter.list_fields.lastname') => $this->request->lastname
]);
}
}
回答by Adnan Mumtaz
Request
is not serializable there is a workaround what you are trying to achieve
Request
不可序列化 有一种解决方法是您要实现的目标
public function store(StoreNewsletterRequest $request)
{
StoreNewsletterJob::dispatch($request->all());
return view('backend.dashboard.index');
}
Your job handler.
您的作业处理程序。
public function handle()
{
if(!Newsletter::isSubscribed($this->request['email']))
{
Newsletter::subscribe($this->request['email'], [
config('newsletter.list_fields.firstname') => $this->request->firstname,
config('newsletter.list_fields.lastname') => $this->request->lastname
]);
}
}
Hope this helps
希望这可以帮助
回答by Stan Barrows
I followed another approach, Just to may help you out!
我采用了另一种方法,只是为了可以帮助您!
Controller
控制器
$newsletter = (object) array(
'email' => $request->email,
'firstname' => $request->firstname,
'lastname' => $request->lastname,
);
StoreNewsletterJob::dispatch($newsletter);
Job
工作
protected $newsletter;
public function __construct( object $newsletter)
{
$this->newsletter = $newsletter;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if(!Newsletter::isSubscribed($this->newsletter->email))
{
Newsletter::subscribe($this->newsletter->email, [
config('newsletter.list_fields.firstname') => $this->newsletter->firstname,
config('newsletter.list_fields.lastname') => $this->newsletter->lastname
]);
}
}