donglv1831 2017-02-24 16:36
浏览 74
已采纳

如何从ajax中获取laravel中的请求

I'm trying to send multiple data to store more tha 1 row in a data base in Laravel, for that I'm using ajax as follow:

var postData = [];

for (i = 0 ; i<100; i++){             
    postData.push({form_name: name[i], etc...});
}

$.ajax({
    async: false,
    url: {{route(createForm)}},
    headers: {"X-CSRF-TOKEN": token},
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(postData),
}); 

When I check de console the data it's being send pretty good but I don't have any clue in how to fetch the request in the controller, when it is just a single request the controller works perfect like this:

public function createForm(Request $data, User $user)
{
    if ($data->ajax()){
        $form=new Form($data->all());
        $user->forms()->save($form); 
    }
} 

But as I said, I don't know if the request is now an array or how can I handle it to store all the data in the DB with a loop

Thanks a lot.

  • 写回答

1条回答 默认 最新

  • dongshou1856 2017-02-24 17:21
    关注
    public function createForm(Request $data, User $user)
    {
        foreach( $data->all() as $row) {
            $form = new Form($row);
            $user->forms()->save($form); 
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题