2017-08-26 125 views
1

我已經jQuery的產生多個表單字段,但只有最後一個記錄保存到數據庫如何使用laravel 5.4

<tr> 
    <td><input class="form-control" id="quantity[]" name="quantity[]" placeholder="Quantity" type="text"></td> 
    <td><input class="form-control" id="price[]" name="price[]" placeholder="Enter Pice" type="text"></td> 
</tr> 

產生額外的表單字段jQuery的多個表單字段保存到數據庫是這裏

$('#priceTable').append(
    "<tr> <td><input class='form-control' id='quantity[1]' name=\"quantity[]\" placeholder='Quantity' type='text'></td><td><input class='form-control' name=\"price[]\" id='price[1]' +' placeholder='Enter Pice' type='text'></td> </tr>"\ 
); 

從PHP頁面上的結果低於

array([quantity] => Array ([0] => 45 [1] => 60) [price] => Array ([0] => 45000 [1] => 60000) 

即假設環秀將其發佈到數據庫中,但只發佈一個數據庫

for ($i=0; $i <= $noQuantity; $i++) 
     { 
      if(!empty($price)){ 
       $data = [ 
        'price' => $priceRe[$i], 
        'quantity' => $quantity[$i] 
       ]; 
       $price->create($data); 
       return redirect('/admin123/category'); 
      } 

     } 

回答

0

這只是一個創造,因爲你在循環內重定向,因此它創建的第一個,然後向用戶發送到其他頁面。嘗試將它:

for ($i=0; $i <= $noQuantity; $i++) 
{ 
    if(!empty($price)){ 
     $data = [ 
      'price' => $priceRe[$i], 
      'quantity' => $quantity[$i] 
     ]; 
     $price->create($data); 
    } 
} 
return redirect('/admin123/category'); 
+0

它工作得很好,但得到**(1/1)ErrorException未定義抵消:已發佈到數據庫 – codePhree

+0

這是環路上的一個錯誤**後3。你從0到'$ noQuantity',我假設它是數組的長度。所以,你要超過最大指數。這樣做:'for($ i = 0; $ i <= $ noQuantity - 1; $ i ++)'或'for($ i = 0; $ i = $ noQuantity - 1; $ i ++)' – ishegg

+0

很高興能幫到您! :) – ishegg