charlieBrown's avatar

Uploading multiple files not passing foreach

I'm here after a lot of search and I have not come to a fix yet. I'm trying to upload several files but it's not getting pass the foreach loop.

/*
         * Validator here. The 'file.*' indicates it's an array.
         */
        $this->validate($request, [
            'file'        => 'required',
            'file.*'      => 'image|mimes:jpg,png|max:5000',
        ]);

        if($request->hasFile('file')){
            $images = $request->file('file');
            //I have confirmed that the code doesn't enter the foorloop and the $images is not empty
            foreach($images as $image){
                $filename = time().'.'.$image->getClientOriginalExtension();
                Image::make($image)->resize(300,300)->save(public_path('images/'.$filename));
            }
        }
$request->session()->flash('alert-success', 'House uploaded sucessully. After validation you will receive an email.');
        return view('addHouse', array('user' => Auth::user()));
0 likes
7 replies
charlieBrown's avatar

@Snapey Why would dumping the variable and halting the script helped me on this case? Any idea?

Snapey's avatar

it would allow you to review if you do have an array or if your form needed tweaking

ErikRobles's avatar

Hello @snapey. I am getting the same issue. I ran the dd($uploadedFiles) which is the name I am using in my case and my dd came back null. Which I would expect since this is the first image. I am using Laravel 7. Anything change since the 5.4 Laravel version that would point me in the right direction? Thanks.

mauricioinaz's avatar

I was having the same issue (Was reading single object instead of the array I was sending). It was solved by changing the name of the request parameter from files to files[].

3 likes

Please or to participate in this conversation.