I am trying to insert several data from an array but at the time of inserting it only inserts the last one I type.
This is my front-end code where I send the data
let payload = new FormData();
for(var i = 0; i< this.addresses.length; i++) {
payload.append('address', this.addresses[i].Adress)
payload.append('city', this.addresses[i].City)
payload.append('country', this.addresses[i].State)
}
payload.append("customer_number", this.customer_number);
payload.append("documents_email", this.documents_email);
payload.append("address_address", this.address_address);
payload.append("tenant_id", this.id);
payload.append("address_countries", this.selectedStateAddress);
payload.append("address_cities", this.selectedCityAddress);
payload.append("name", this.name);
payload.append("abbr", this.abbr);
payload.append("email", this.email);
payload.append("phone", this.phone);
payload.append("contact", this.contact);
payload.append("chamber_commerce", this.chamber_commerce);
payload.append("vat", this.vat);
this.$http.post("/saveTenant", payload)
.then(res => {
this.datatenants = res.data.data;
console.log("The client save");
});
This is my Backend code where I am receiving the data
$address = $request ->getPost('address', 'string');
$country_id = $request ->getPost('country', 'int');
$city_id = $request ->getPost('city', 'int');
$addresses = new TenantAddresses();
$addresses->address = $address;
$addresses->country_id = $country_id;
$addresses->city_id = $city_id;
if ($addresses->save() === false) {
$this->db->rollback();
return;
}
This is what arrives in the data at the time of saving
That is the last data that I add, it is the only one that is saving
I do not know which is the correct way so that when I add more data to save it correctly, I hope that my doubt has been clear and you can collaborate with me.
I get constructive criticism as I am a bit new to programming. Thanks a lot.
payload
does not contains an array of addresses. I would check the functionappend
of the classFormData
. I think theappend
function writes only one element at the time. Instead what you need is to send an array.