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

爱⌒轻易说出口 提交于 2021-02-07 07:15:26

问题


I try to send attachement files but i get

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

I use 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('test@gmaIL.com')->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;
    }

回答1:


This is because $request->file('uploads') returns an object and you're trying iterate over it with 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)


来源:https://stackoverflow.com/questions/47609960/cannot-use-object-of-type-illuminate-http-uploadedfile-as-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!