2016-12-02 48 views
0

私は以下の画像のように複数の入力フォームを作成しましたが、データベースに保存したいのですが、問題はdd()を使用して2行目のデータのみが表示されます。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

あなたはこれでマルチアイテムを挿入することができますコード:public function store(Request $request)

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

ここ

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が正しく動作していることを考慮して、上記の修正により、データベースの各行のデータが保存されます。

関連する問題