2016-06-11 15 views
2

これで数週間、私はLaravelフレームワークを学びましたが、Laravelでどのように正しく同じ名前のフィールドを挿入したフォームを送信するかを知りたいデータベースに保存する方法。そこで、以下のコードの一部です:Laravelコードの同じ名前の複数の入力フィールドからmysqlを挿入する方法

<tr> 
    <td> 
     <div class="form-group{{ $errors->has('item') ? ' has-error' : '' }}"> 
      <label for="item"></label> 
      <input type="text" class="form-control" id="item" name="item[]" placeholder="Prekė"> 
     </div> 
    </td> 
    <td> 
     <div class="form-group{{ $errors->has('quantity') ? ' has-error' : '' }}"> 
      <label for="quantity"></label> 
      <input type="text" class="form-control quantity" name="quantity[]" placeholder="Kiekis"> 
     </div> 
    </td> 
    <td> 
     <div class="form-group{{ $errors->has('price') ? ' has-error' : '' }}"> 
      <label for="price"></label> 
      <input type="text" class="form-control price" name="price[]" placeholder="Kaina"> 
     </div> 
    </td> 
</tr> 
<tr> 
    <td> 
     <div class="form-group{{ $errors->has('item') ? ' has-error' : '' }}"> 
      <label for="item"></label> 
      <input type="text" class="form-control" id="item" name="item[]" placeholder="Prekė"> 
     </div> 
    </td> 
    <td> 
     <div class="form-group{{ $errors->has('quantity') ? ' has-error' : '' }}"> 
      <label for="quantity"></label> 
      <input type="text" class="form-control quantity" name="quantity[]" placeholder="Kiekis"> 
     </div> 
    </td> 
    <td> 
     <div class="form-group{{ $errors->has('price') ? ' has-error' : '' }}"> 
      <label for="price"></label> 
      <input type="text" class="form-control price" name="price[]" placeholder="Kaina"> 
     </div> 
    </td> 
</tr> 

ワンピース:

public function postNewOrder(Request $request) 
{ 
    $this->validate($request, [ 
     'title' => 'required', 
     'item' => 'required', 
     'quantity' => 'required', 
     'price' => 'required', 
    ]); 

    Order::create([ 
     'user_id' => Auth::user()->id, 
     'title' => $request->input('title'), 
     'total' => $request->input('total'), 
    ]); 

    Item::create([ 
     'order_id' => 1, 
     'position' => 1, 
     'item' => $request->input('item'), 
     'quantity' => $request->input('quantity'), 
     'price' => $request->input('price'), 
     'sum' => $request->input('sum'), 
    ]); 

    return redirect()->route('order.list')->with('info', 'Užsakymas sėkmingai pridėtas'); 
} 

私はforeachのでループする必要があることを知っているが、私はこれを行うにはどのようには考えています。

ありがとうございました!

EDIT: https://jsfiddle.net/xqy6qafk/3/

あなたが "Pridėtiprekę" ボタンをクリックしてください:ここで は一例です。 1つの行がテーブルに追加されます。だから今私はこれらの2行を "Išsaugoti"ボタンをクリックしたときにデータベースに値を挿入したいと思っていますが、これを行う方法はわかりません。

+0

あなたの質問は非常に明確ではありません。あなたはあなたが持っているものと達成したいものの本当の例を挙げることができますか? –

+0

@AlexeyMezenin編集しました。あなたが私を理解することを願っています:) – Sprunkas

答えて

0

jQueryを使用して動的に行を追加するので、jQueryを使用して動的に作成された行からデータを収集し、このデータをコントローラに送信することをお勧めします。

コントローラでは、JSONデータが準備されていれば、json_decode()機能を持つ配列の配列(行の配列)に変換できます。

あなたはどのforeachループせずに、単一の句を使用して複数の行を挿入できることをやった後:

Item::create($arrayOfArrays); // This will insert multiple items 
関連する問題