Fabrizio Fenoglio
Fabrizio Fenoglio

Reputation: 5947

Passing object on Queue as data laravel 4 / PHP

I created a class that is responsible to resize images once it is uploaded. This class will be used with a queue visor. Having this class in my development project the default settings of the queue is set to sync.

The queue work fine, but the big issue unexpected is that passing a object on the array of data, when I get it on my class handler for the queue it because an empty array.

This sort of "serialization" of the object break all my logic for implement this awesome class I made.

I would like ask if this behaviour is normal, if yes how can be a work around to pass object as data in my queue class?

This is how I pass the object on my handlerQueue class

$file = Input::file('file');
$image = new Image($file);
Queue::push('HandlerQueue',['image' => $image]);


class HandlerQueue
{
   public function fire($job,$data)
   {
       dd($data['image']); // Empty array :(
   }

}

Any help will be really appreciated.

Upvotes: 2

Views: 1414

Answers (1)

Laurence
Laurence

Reputation: 60040

You cant pass an object onto the queue without serialization.

What you could do is pass a reference to the object, and call it again. Like this (pseudocode):

$file = Input::file('file');
$image = new Image($file);
$image_id = save $file and get ID  // save reference
Queue::push('HandlerQueue',['image_id' => $image_id]);


class HandlerQueue
{
   public function fire($job,$data)
   {
       $image = new Image($data['image_id']);  // use the reference and recreate the object
   }

}

Upvotes: 4

Related Questions