循环遍历请求数据数组并将每组保存到数据库laravel

时间:2015-10-11 11:47:12

标签: mysql database laravel laravel-5 eloquent

我有这个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>

我有一个专栏&#39;孩子&#39;&#39; type_of_delivery&#39;&#39; date&#39;对于那些请求数据。我希望像每组一样保存它,例如子类的数组0 + type_of_delivery的数组0 +数组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

任何想法,线索,帮助如何取悦它?

1 个答案:

答案 0 :(得分:-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之间的结果相同的情况。 var nonparent = $('.notParent'); var position = nonparent.offset(); $('.child1').offset({ top: position.top, left: position.left });