【问题标题】:how to send multiple sets of data input with request HTTP in Laravel?如何在 Laravel 中使用请求 HTTP 发送多组数据输入?
【发布时间】:2017-11-08 01:11:10
【问题描述】:

我正在尝试使用 Laravel 制作一个发送两组数据的表单。 我的表格是这样的。

<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 将保存到另一个表“电话”。 好的!问题是我想让用户制作尽可能多的phoneField(当然是使用Javascript)。 所以我认为最好的方法是像这样保存nameField:

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

但是phoneField呢?如何拯救他们?用户可以制作大约 10 个字段或更多。有没有办法将 phoneFields 分组为任何数组并通过请求 HTTP 发送数组?

【问题讨论】:

    标签: php laravel http request


    【解决方案1】:

    您可以发送一组phoneField。 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...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2017-04-21
      • 2011-05-26
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多