jeudi 18 octobre 2018

Laravel: Invalid argument supplied for foreach() when sending values with multipart-form.data

I'm trying to access an array on a multipart-form-data request.

On my js code I add to the formData the object that I need this way:

for (const key in property) {
    if (typeof property[key] !== 'undefined') {
        console.log(key, property[key]);
        formData.append(`property[${key}]`, property[key]);
    }
}

Also in the same forData I send all the data from the images that I want to handle, and in this case the property contains an array of objects that are images previously stored in the database. So, in my controller I have to following

 // Images that are already in database
 $propertyData = $request->input('property');
 $images = isset($propertyData['images'])? $propertyData['images'] : []
 foreach($images as $i) {
    $image = Image::find((int)$i['id']);
    if ( !is_null($image) ) {
        $image->update($i);
    }
}

But I got the error: Invalid argument supplied for foreach()

I've tried to even convert each image on the javascript code to an array like the property, but i got the same error.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire