2016-12-02 87 views
0

我已經創建多重輸入表單像下面的圖像,我要保存到數據庫中,當我使用dd()這只是僅顯示在第二行數據的問題。Laravel插入多個輸入表單數據庫

我怎樣才能節省每一行數據庫?

enter image description here

這裏我laravel控制器

public function store(Request $request) 
{ 

     $input = Input::all(); 
     $condition = $input['service_id']; 
     foreach ($condition as $key => $condition) { 
      $detailorder = new DetailOrder; 
      $detailorder->serivce_id = $input['service_id'][$key]; 
      $detailorder->order_type = $input['order_type'][$key]; 
      $detailorder->select_plan = $input['select_plan'][$key]; 
      $detailorder->qty = $input['qty'][$key]; 
      $detailorder->unit_price = $input['unit_price'][$key]; 
      //$detailorder->mandays = $input['mandays'][$key]; 
      $detailorder->note = $input['note'][$key]; 
      } 
      dd($detailorder); 

} 

,在這裏我表腳本,我使用jQuery創建它

function getCorporateService(id){ 
    // get data and parsing to column 
    $.get("{{ url('salesorder/service')}}/"+id, function(data){ 
     console.log(id); 
     console.log(data); 

     $.each(data, function (index, element){ 
      $br = "<tr id='item'>"; 
      $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier[]' readonly></td>"; 
      $br += "<td><input class='input-small' type='text' id='service_name["+id+"]' name='service_name[]' value='"+element.service_name+"' readonly>" 
         +"<input class='input-small' type='hidden' id='service_id["+id+"]' name='service_id[]' value='"+element.id+"' readonly></td>"; 
      $br += "<td><select id='order_type["+id+"]' name='order_type[]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>"; 
      $br += "<td><input class='input-small' type='text' id='select_plan["+id+"]' name='select_plan[]'></td>"; 
      $br += "<td><input class='input-mini' type='text' id='qty["+id+"]' name='qty[]' value='1' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='unit_price["+id+"]' name='unit_price[]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><input class='input-small' type='text' id='total_price["+id+"]' name='total_price[]' onChange='getTotalPrice("+id+")'></td>"; 
      $br += "<td><textarea class='input-small' id='notes["+id+"]' name='note[]'></textarea></td>"; 
      $br += "</tr>"; 

      $(".corporatesvc").append($br); 

     }); 
    }); 
} 

回答

1

我不知道你的dd功能。目前,循環運行,並消失了你的老訂單和傳遞您的dd function最後一個,所以你需要創建一個對象數組,並傳遞它的功能一樣,

$allOrders=array(); // make an array, for storing all detail order in it 
foreach ($condition as $key => $condition) { 
    $detailorder = new DetailOrder; 

    //you can use to ignore $key::=> $detailorder->serivce_id = $condition['service_id']; 

    $detailorder->serivce_id = $input['service_id'][$key]; 
    $detailorder->order_type = $input['order_type'][$key]; 
    $detailorder->select_plan = $input['select_plan'][$key]; 
    $detailorder->qty = $input['qty'][$key]; 
    $detailorder->unit_price = $input['unit_price'][$key]; 
    //$detailorder->mandays = $input['mandays'][$key]; 
    $detailorder->note = $input['note'][$key]; 
    $allOrders[]=$detailorder; 
} 
dd($allOrders); // pass it to the function 
+0

謝謝。它現在的工作:) – rafitio

1

檢查以下行:

foreach ($condition as $key => $condition) { 
    $detailorder = new DetailOrder; 
    // here you are creating a new object on each iteration, means on each iteration new object overrides old one, that's why you are getting the last record only 
} 

要解決此問題,請將此對象創建代碼放置在循環外並再次嘗試。

1

可以插入多項目與此代碼:

DB::table('tablename')->insert($tableitems); 
2

public function store(Request $request)添加行如下所示這裏

public function store(Request $request) 
{ 

     $input = Input::all(); 
     $condition = $input['service_id']; 
     foreach ($condition as $key => $condition) { 
      $detailorder = new DetailOrder; 
      $detailorder->serivce_id = $input['service_id'][$key]; 
      $detailorder->order_type = $input['order_type'][$key]; 
      $detailorder->select_plan = $input['select_plan'][$key]; 
      $detailorder->qty = $input['qty'][$key]; 
      $detailorder->unit_price = $input['unit_price'][$key]; 
      //$detailorder->mandays = $input['mandays'][$key]; 
      $detailorder->note = $input['note'][$key]; 

      //for saving each DetailOrder to database 
      $detailorder->save(); 
      } 
} 

注意,save()應從foreach循環中調用的每一行保存到數據庫中。

考慮到你的JavaScript是否工作正常,上述修改將在數據庫中保存的每一行的數據。