dongya2029 2018-10-18 22:47
浏览 156
已采纳

Laravel:使用multipart-form-data发送值时为foreach()提供的参数无效

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]);
    }
}

The property has the array images and the value is an array like this:

enter image description here

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.

  • 写回答

1条回答 默认 最新

  • dongpu1881 2018-10-18 23:19
    关注

    You cannot directly append objects or arrays to FormData. You must add the keys and values one by one.

    A way to do that is like so:

    var formData = new FormData;
    var arr = ['this', 'is', 'an', 'array'];
    for (var i = 0; i < arr.length; i++) {
        formData.append('arr[]', arr[i]);
    }
    

    I use a helper function (note that this is in ES6 syntax) to do this:

    export function appendFormdata(formData, data, previousKey) {
        if (data instanceof Object) {
            Object.keys(data).forEach(key => {
                const value = data[key];
                if (previousKey) {
                    key = `${previousKey}[${key}]`;
                }
                if (value instanceof Object && !Array.isArray(value) && !(value instanceof File)) {
                    return appendFormdata(formData, value, key);
                }
                if (Array.isArray(value)) {
                    value.forEach((val, i) => {
                        return appendFormdata(formData, val, `${key}[${i}]`);
                    });
                } else {
                    formData.append(key, value);
                }
            });
        }
    }
    

    For more details refer: appending array to FormData and send via AJAX and Convert JS Object to form data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵