이제 몇 주 동안 나는 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"버튼을 클릭 할 때이 두 행을 값으로 데이터베이스에 삽입하려고합니다. 그러나 이것을 수행하는 방법을 모르겠습니다.
귀하의 질문은 매우 명확하지 않다. 당신이 갖고있는 것과 달성하고자하는 것에 대한 진정한 본보기를 줄 수 있습니까? –
@AlexeyMezenin 내 게시물을 편집했습니다. 당신이 나를 이해할 수 있기를 바랍니다 :) – Sprunkas