当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to fetch a request in laravel from ajax

问题描述:

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.

网友答案:
public function createForm(Request $data, User $user)
{
    foreach( $data->all() as $row) {
        $form = new Form($row);
        $user->forms()->save($form); 
    }
} 
分享给朋友:
您可能感兴趣的文章:
随机阅读: