17

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. Without any success.

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

            ]);
        }
    }
Stan Barrows
  • 873
  • 1
  • 12
  • 27
  • 1
    It needs to serialize objects into wherever your sending the jobs so they can be reconstructed when the job runs. The Request class isn't serializable hence why you get the error. Models are serializable by default so work without problems. There's a few ways you could fix it. The obvious one is to make sure the object you pass in is serializable but If your only interested in those 3 attributes the easy fix would be to pass them into the constructor ignoring objects altogether. – Dave Carruthers Mar 07 '18 at 17:51

3 Answers3

19

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

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
2

Another possibility for this if you still want access to many of the familiar methods 'only, get' etc you can just

collect($request->all());

And use it as a collection instead

MHewison
  • 846
  • 10
  • 17
1

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

            ]);
        }
    }
Stan Barrows
  • 873
  • 1
  • 12
  • 27