Laravel Jobs Serialization of 'Closure' is not allowed

后端 未结 2 2285
轮回少年
轮回少年 2021-02-20 05:55

I would like to send Data to a NewsletterStore Job. But it\'s failing with the following error. Any suggestions?

I also tried to remove the SerializesModels Models trait

相关标签:
2条回答
  • 2021-02-20 06:14

    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
    
                ]);
            }
        }
    
    0 讨论(0)
  • 2021-02-20 06:34

    Request is not serializable there is a workaround what you are trying to achieve

     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

    0 讨论(0)
提交回复
热议问题