0

I have an image uploader that allows multiple images on the frontend (vue.js). When I send it over to Laravel, it's not hitting my foreach loop. I traced the print logs to be executing up until the foreach loops runs but I'm not sure why it's not going through each one unless that's not the correct way.

** JS **

let formData = new FormData();
this.files.forEach((x,index) => {
    formData.append("file", x)
});

axios.post('/admin/upload', formData, {
    headers: {
        'Content-Type': 'multipart/form-data',
    }
})

**Laravel **

print "outside";
if ($request->hasFile('file')) {
    print "inside";
    $files = $request->file('file');
    $stack = [];
    foreach ($files as $file) {
        print "Looping";
        $fileName = Storage::put('/trace/', file_get_contents($file->getRealPath()), ['visibility' => 'public']);
        array_push($stack, $fileName);
    }
    return response()->json($stack);
}
1
  • dd the $files variable, and check if you are sending the files to the server. Commented Apr 30, 2020 at 16:49

1 Answer 1

1

As you are uploading multiple files the file formdata key should actually be an array like this - formData.append("file[]", x)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.