Lars-Janssen's avatar

Laravel job queue fails

hi,

I've created a job that uploads file:

class UploadFiles implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * @var
     */
    protected $request;
    /**
     * @var
     */
    protected $directory;
    /**
     * @var
     */
    protected $context;

    /**
     * UploadFiles constructor.
     * @param $request
     * @param $directory
     * @param $context
     */
    public function __construct($request, $directory, $context)
    {
        $this->request      = $request;
        $this->directory    = $directory;
        $this->context      = $context;
    }

    /**
     *
     */
    public function handle()
    {
        foreach ($this->request->attachment as $attachment) {
            $fileName = $this->context->generateFileName($attachment);
            $attachment->storeAs('users/' . Auth::user()->id . '/' . $this->directory, $fileName, 's3');
            $this->context->saveFile($fileName);
        }
    }
}

But I receive the error:

Exception in Queue.php line 86:
Serialization of 'Closure' is not allowed

I'm using beanstalkd on forge. Why do I get this error??

--EDIT--

So it looks like I cannot pass a Request object or a Illuminate\Http\UploadedFile so how would I push a file to a queue?

0 likes
5 replies
M4rk3tt0's avatar

Try this:

    public function handle()
    {
        foreach ($this->request->attachment as $attachment) {
            $fileName = $this->context->generateFileName($attachment); // you are missing $this
            $attachment->storeAs('users/' . Auth::user()->id . '/' . $this->$directory, $fileName, 's3');
            $this->context->saveFile($fileName);
        }
    }
Lars-Janssen's avatar

I think it's not possible to send an instance of Illuminate\Http\Request; to a job?

M4rk3tt0's avatar

No, it seems that it's not possible. But you should be able to send $request->all()

Lars-Janssen's avatar

Yes, $request->all() is working. But then I receive another error: Serialization of 'Illuminate\Http\UploadedFile' is not allowed

Please or to participate in this conversation.