Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel queue error Serialization of 'Illuminate\Http\UploadedFile' is not allowed

Tags:

php

laravel

I am trying to upload a file using a queue(bean) in laravel but I get this error: Serialization of 'Illuminate\Http\UploadedFile' is not allowed

My code is:

    protected $file;
    protected $Id;

public function __construct($file,$Id)
    {
        $this->file = $file
        $this->Id = $Id;
    }

public function handle()
    {
        $qFile = $this->file;
        $qId = $this->Id;

        $s3 = Storage::disk('s3');
        $extension = $qFile->guessExtension();
        $filename = uniqid().'.'.$extension;

      //Create and resize images
      $image = Image::make($qFile)->resize(null, 600, function ($constraint) {
          $constraint->aspectRatio();
      });
        $image->encode($extension);

        $imageLarge = Image::make($qFile)->resize(null, 800, function ($constraint) {
          $constraint->aspectRatio();
      });
        $imageLarge->encode($extension);

      // upload image to S3
      $s3->put("images/{$qId}/main/".$filename, (string) $image, 'public');
        $s3->put("images/{$qId}/large/".$filename, (string) $imageLarge, 'public');

      // make image entry to DB
      File::create([
          'a_f_id' => $qId,
          'file_name' => $filename,
      ]);
    }

But if I remove:

protected $file; protected $Id;

I dont get the error

like image 455
user2636197 Avatar asked Nov 11 '16 17:11

user2636197


1 Answers

You can’t pass an uploaded file instance to a job. You need to write it to disk somewhere, and then retrieve it when handling the job.

like image 193
Itamar Garcia Avatar answered Sep 22 '22 00:09

Itamar Garcia