如何在Laravel中使用请求HTTP发送多组数据输入? [英] how to send multiple sets of data input with request HTTP in Laravel?

查看:76
本文介绍了如何在Laravel中使用请求HTTP发送多组数据输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel创建一个发送两组数据的表单. 我的表格是这样的.

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>

第一个放入的nameField将保存到表"names". phoneField将保存到另一个表"phones". 好的!问题是我想让用户制作尽可能多的phoneField(当然使用Javascript). 所以我认为最好的方法是这样保存nameField:

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")]);

但是phoneField呢?如何保存它们?用户可以制作大约10个字段或更多.有什么方法可以将phoneFields分组为任何数组,并使用请求HTTP发送该数组?

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?

推荐答案

您可以发送phoneField组.将数组用于phoneField

You can send group of phoneField. Use array for phoneField

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

在您的控制器中

$data = $request->all();
$phoneFields = $data['phoneField'];
foreach($phoneFields as $phoneField)
{
    //implement to save phone number...
}

这篇关于如何在Laravel中使用请求HTTP发送多组数据输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆