【问题标题】:loop through an array of request data and save per set to the database laravel遍历请求数据数组并将每组数据保存到数据库 laravel
【发布时间】:2016-01-08 22:57:33
【问题描述】:

我有这个 var_dump 结果

'children' => 
    array (size=2)
        0 => string 'children on row 1' (length=17)
        1 => string 'children on row 2' (length=17)
'type_of_delivery' => 
    array (size=2)
        0 => string 'type of delivery on row 1' (length=25)
        1 => string 'type of delivery on row 2' (length=25)
'date' => 
    array (size=2)
        0 => string 'Oct 11, 2015' (length=12)
        1 => string 'Oct 11, 2015' (length=12)

来自我的输入

<table>
    <thead>
        <tr>
            <th>Children</th>
            <th>Type of Delivery</th>
            <th>Date</th>
        <tr>
    <thead>
    <tbody>
        <tr>
            <td><input type="text" name="children[]" /></td>
            <td><input type="text" name="type_of_delivery[]" /></td>
            <td><input type="text" name="date[]" /></td>
        </tr>
        <tr>
            <td><input type="text" name="children[]" /></td>
            <td><input type="text" name="type_of_delivery[]" /></td>
            <td><input type="text" name="date[]" /></td>
        </tr>
    </tbody>
</table>

对于这些请求数据,我有一个列“children”、“type_of_delivery”、“date”。我想像每组一样保存它,例如数组 0 的孩子 + 数组 0 的 type_of_delivery + 数组 0 日期等等。

假设我已经成功保存了这些数据,它应该看起来像这样。

--------------------------------------------------------------
     children     |    type of delivery       |  date
--------------------------------------------------------------
children on row 1 | type of delivery on row 1 | Oct 11, 2015
children on row 2 | type of delivery on row 2 | Oct 11, 2015

任何想法,线索,请帮助如何制作?

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: mysql database laravel laravel-5 eloquent


【解决方案1】:

如果您的数组保持相同的格式,您可以执行以下操作:-

$children = $arr['children'];

for (i=0; i < count($children); i++)
{
    $update = [
        'children' => $children[$i],
        'type_of_delivery' => $arr['type_of_delivery'][$i], 
        'date' => $arr['date'][$i],  
    ];

    Your\Model\To\Insert::create($update);
} 

这只有在 childrendeliverydate 的结果相同时才有效

【讨论】:

  • 我不太明白。你能解释一下你上面的代码吗?
猜你喜欢
  • 2018-01-31
  • 2021-02-15
  • 2019-05-13
  • 2022-11-14
  • 2013-06-07
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多