Laravel Jobs不允许“关闭”序列化

时间:2018-03-07 17:25:50

标签: php laravel jobs

我想将数据发送到NewsletterStore作业。但它失败了以下错误。有什么建议吗?

我还尝试删除SerializesModels Models特征。没有任何成功。

错误

Exception
Serialization of 'Closure' is not allowed

控制器

 public function store(StoreNewsletterRequest $request)
    {
        StoreNewsletterJob::dispatch($request);

        return view('backend.dashboard.index');
    }

作业

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

            ]);
        }
    }

3 个答案:

答案 0 :(得分:4)

Request不可序列化有一种解决方法,您正在尝试实现

 public function store(StoreNewsletterRequest $request)
{
    StoreNewsletterJob::dispatch($request->all());

    return view('backend.dashboard.index');
}

你的工作经理。

 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

        ]);
    }
}

希望这有帮助

答案 1 :(得分:0)

我采用了另一种方法,只是为了帮助你!

<强>控制器

  $newsletter = (object) array(

                'email' => $request->email,
                'firstname' => $request->firstname,
                'lastname' => $request->lastname,

            );

   StoreNewsletterJob::dispatch($newsletter);

<强>作业

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

            ]);
        }
    }

答案 2 :(得分:0)

如果您仍然想访问许多熟悉的方法“only,get”等,您可以使用另一种可能性

collect($request->all());

并将其用作集合