2015-10-11 29 views
2

我有這樣的var_dump結果循環通過請求數據的陣列,並且每個組保存到數據庫laravel

'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> 

和我有一個列「孩子」,「 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 

有什麼想法,線索,如何讓它好用?

+0

那你試試這樣遠? – Alex

回答

-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); 
} 

此,如果您在children有平等的結果只會工作,delivery & date

+0

我不太明白這一點。你能解釋一下你的代碼嗎? –

相關問題