Laravel队列错误:“不允许对‘Illuminate\Http\UploadedFile’进行序列化”。

6

我正在尝试使用Laravel中的队列(bean)上传文件,但是出现了这个错误:不允许对'Illuminate\Http\UploadedFile'进行序列化

我的代码如下:

    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,
      ]);
    }

如果我删除:

protected $file; protected $Id;

那么我就不会收到错误提示。


你尝试将 protected $file 改为 private $file 了吗? - Eric Tucker
1个回答

11

您无法将上传的文件实例传递给作业。您需要将其写入磁盘的某个位置,然后在处理作业时检索它。


最佳答案!谢谢! - Renan Coelho
6
可以给我们举个例子吗? - dipenparmar12
@RenanCoelho 那个答案只是从 https://laracasts.com/discuss/channels/laravel/serialization-of-illuminatehttpuploadedfile-is-not-allowed-on-queue 复制粘贴过来的。 - Pinnokkio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接