how to send multiple sets of data input with reque

2019-08-02 14:57发布

Im trying to make a form with Laravel that send two sets of data. my form is like this.

<form>
  <fieldset method="POST" action"some url">
    <label for="nameField">Name</label>
    <input type="text" id="nameField" name="nameField">

    <label for="phoneField">Name</label>
    <input type="text" id="phoneField" name="phoneField">


    <input class="button-primary" type="submit" value="Send">
  </fieldset>
</form>

the first in put nameField will save to the table "names". the phoneField will save to another table "phones". Ok! the problem is I want to let user make as many phoneField as they want (with Javascript of course). so i think the best way is i save nameField like this:

Name::create([ "name" => request("nameField")]);

but how about phoneField? how to save them? users can make about 10 field or more. is there any way to group the phoneFields as any array and send the array with request HTTP?

1条回答
别忘想泡老子
2楼-- · 2019-08-02 15:26

You can send group of phoneField. Use array for phoneField

<label for="phoneField">Name</label>
<input type="text" id="phoneField" name="phoneField[]">

And in your controller

$data = $request->all();
$phoneFields = $data['phoneField'];
foreach($phoneFields as $phoneField)
{
    //implement to save phone number...
}
查看更多
登录 后发表回答