I’m trying to upload multiple images in one request with Laravel. However, it only seems to be seeing one file regardless of the fact that my file input has multiple files attached.
Markup:
<input class="file-input" type="file" name="fileUpload" multiple>
Controller:
$this->validate($request, [
'fileUpload' => 'image|nullable',
...
foreach ($request->file('fileUpload') as $image) {
dd('stop');
}
dd('fail');
In this case, it always returns "fail". However, if I try to just $request->file('fileUpload')
it will return to me a single file, like so:
2
Answers
Like multiple selects, if you want to send multiple values then you need to use the array syntax in your input’s
name
attribute. So it should befileUpload[]
instead of justfileUpload
Change the name of the input fileUpload to fileUpload[] :
Also in the controller for validating multiple fields, change ‘fileUpload’ to ‘fileUpload.*’ :