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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试发送多个数据来存储Laravel数据库中的更多1行,为此我 使用ajax如下:

  var postData = []; 
 
for(i = 0; i&lt; 100; i ++){
 postData.push({  form_name:name [i],etc ...}); 
} 
 
 $ .ajax({
 async:false,
 url:{{route(createForm)}},
 headers:{  “X-CSRF-TOKEN”:令牌},
类型:'POST',
 contentType:'application / json',
 dataType:'json',
 data:JSON.stringify(postData),
  });  
   
 
 

当我检查de控制台时它发送的数据相当不错,但我没有任何关于如何在控制器中获取请求的线索,当它是 只需一个请求,控制器就可以完美地运行:

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

但正如我所说,我不知道请求现在是一个数组还是我如何处理它来存储所有 带循环的数据库中的数据

非常感谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题