Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lars-Janssen's avatar

FormData becomes an object instead of array?

Hi,

In my app I'm sending form data and append it like this (in vue.js):

let form = new FormData();
form.append('locations[]', this.form.locations);

Then I send it with axios to the server.

The problem is that on my server I receive objects instead of an array.

When I send it without the FormData it looks like this:

How do I get the array data instead of objects?

0 likes
7 replies
rawilk's avatar

You need to iterate over the locations and add each one to the FormData.

2 likes
Lars-Janssen's avatar

@wilk_randall already tried that:

for (var i = 0; i < this.form.locations.length; i++) {
    form.append('locations[]', this.form.locations[i]);
}

But that's not working.

rawilk's avatar

Try this instead:

this.form.locations.forEach((location, i) => form.append(`locations[${i}]`, location));
2 likes
geneowak's avatar

@lars-janssen this an old question but I think this might be what you were working on...

I am guessing like me, you were trying to append an array of objects if so this is how I went about it...

Say the array is this

const items = [ {"name": "item 1", "price": 100}, {"name": "item 2", "price": 400}];

you can append it like so

const formData = new FormData();

for (let i = 0; i < items.length; i++) {
  for (let key of Object.keys(items[i])) {
    console.log('hello...', items[i][key]);
    formData.append(`items[${i}][${key}]`, items[i][key]);
  }
}

when you post the formData object, Laravel will be able to pick up the correct array and any form request validation should be able to pass...

1 like
Annajar's avatar

I really wanna thank you for this solution, and I wanna Ask you please, what if I have an array of objects called Questions that has another array of objects called choices what should I do then so I can send my data to the controller ? like this: questions:[ { question_text: null, question_file:null, choices:[{c_text}] }

Please or to participate in this conversation.