laravel 不能使用 Illuminate\Http\UploadedFile 类型的对象作为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47609960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:02:24  来源:igfitidea点击:

Cannot use object of type Illuminate\Http\UploadedFile as array

laravel

提问by Mathieu Mourareau

I try to send attachement files but i get

我尝试发送附件文件,但我得到

Cannot use object of type Illuminate\Http\UploadedFile as array

不能使用 Illuminate\Http\UploadedFile 类型的对象作为数组

I use laravel 5.4

我使用 Laravel 5.4

Someone know why i'm getting this error ?

有人知道为什么我会收到这个错误吗?

( I don't upload the file into a directory, i just want to send the file who was requested on my controller )

(我没有将文件上传到目录中,我只想发送我的控制器上请求的文件)

Hope someone could help , best regards :)

希望有人能帮忙,最好的问候:)

Here my controller :

这是我的控制器:

public function postSendMassive(Request $request){

        $files = $request->file('uploads');

        $emails = Structure::where('type_structure_id', 4)->pluck('adresse_email_structure');

        $subject = $request->subject;
        $bodyMessage = $request->texte;

        foreach($files as $file) {
            $files[] = [
                'file' => $file->getRealPath(),
                'options' => [
                    'mime' => $file->getClientMimeType(),
                    'as'    => $file->getClientOriginalName()
                ],
            ];
        }

        Mail::to('[email protected]')->send(new MassiveEmail($subject , $bodyMessage , $files));

        return back()->with('status', "Email envoyé");

    }

here my build mail :

这是我的构建邮件:

public function build()
    {

        $subject = $this->subject;
        $bodyMessage = $this->bodyMessage;
        $files = $this->files;

        $email =  $this->markdown('email.MassiveMail',compact('bodyMessage'))
            ->subject($subject.'-'.'FFRXIII Licences & Compétitions');


        foreach($this->files as $file) {
            $email->attach($file['file'],$file['options']);
        }

        return $email;
    }

回答by Alexey Mezenin

This is because $request->file('uploads')returns an object and you're trying iterate over it with foreach

这是因为$request->file('uploads')返回一个对象并且您正在尝试使用它进行迭代foreach

If you want to upload multiple files, make sure you're doing something like this:

如果您想上传多个文件,请确保您正在执行以下操作:

<input type="file" name="uploads[]" multiple />

And iterate over uploaded files:

并迭代上传的文件:

foreach ($request->uploads as $file)