简体   繁体   English

将Javascript数组值传递给Laravel Controller

[英]Passing Javascript array value to Laravel Controller

I want to pass/store JavaScript array[totalData] variable to Laravel controller. 我想将JavaScript数组[totalData]变量传递/存储到Laravel控制器。 And How to insert multiple rows to database? 以及如何将多行插入数据库? Please help me. 请帮我。

Js functions Js功能

The easiest is to encode the array in the front end and then decoding it in the backend. 最简单的方法是在前端对数组进行编码,然后在后端对其进行解码。

You can achieve this by: 你可以通过以下方式实现:

Front end: 前端:

let formData = new FormData();
formData.append('data', JSON.stringify(totalData));

// some xmlHttpRequest (eg: axios)
axios.post('/yoururl', formData, headers).then(() => {}).catch(() => {});

Then in the backend, 然后在后端,

public method somefunction(Request $request)
{
    $request->request-set('data', json_decode($request->data));

    // validation and other stuff

    foreach ($request->data as $data)
    {
        /Insert to database
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM