2016-06-11 2 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 문으로 루프해야 할 것을 알고,하지만 난 어떻게하는지 모르겠어요.

미리 도움을 청하십시오!

편집 : https://jsfiddle.net/xqy6qafk/3/

당신은 "Pridėti prekę"버튼을 클릭 : 여기 은 예입니다. 한 행이 표에 추가됩니다. 그래서 "Išsaugoti"버튼을 클릭 할 때이 두 행을 값으로 데이터베이스에 삽입하려고합니다. 그러나 이것을 수행하는 방법을 모르겠습니다.

+0

귀하의 질문은 매우 명확하지 않다. 당신이 갖고있는 것과 달성하고자하는 것에 대한 진정한 본보기를 줄 수 있습니까? –

+0

@AlexeyMezenin 내 게시물을 편집했습니다. 당신이 나를 이해할 수 있기를 바랍니다 :) – Sprunkas

답변

0

동적으로 jQuery를 사용하여 행을 추가하므로 가장 좋은 방법은 동적으로 생성 된 행에서 jQuery로 데이터를 수집 한 다음이 데이터를 컨트롤러로 보내는 것입니다.

컨트롤러에서 JSON 데이터가 준비되면 json_decode() 기능을 사용하여 배열 배열 (행 배열)로 변환 할 수 있습니다.

당신이 어떤 foreach 루프없이 하나의 절을 여러 행을 삽입 할 수 일 후 :

Item::create($arrayOfArrays); // This will insert multiple items 
관련 문제