2017-05-17 70 views
1

我是新的laravel.this是我的看法形式Laravel 5.3 - 插入數據,以多行

<div class="row"> 
    <div class="col-md-6"> 
     <form class="" action="{{route('production.update', $rm->id)}}" method="post"> 
     <input name="_method" type="hidden" value="PATCH"> 
     {{csrf_field()}} 

     <input type="hidden" name="id" id="id" class="form-control" value="{{$rm->id}}"> 

     <div class="form-group{{ ($errors->has('batch_id')) ? $errors->first('title') : '' }}"> 
      <lable>Batch ID</lable> 
      <input type="text" name="batch_id" id="batch_id" class="form-control" value="{{$rm->product_id}}" readonly=""> 
      {!! $errors->first('wastage','<p class="help-block">:message</p>') !!} 
     </div> 

<div class="form-group{{ ($errors->has('')) ? $errors->first('title') : '' }}"> 
      <lable>Packing</lable> 

      @foreach($subitem as $sub) 

      <div class="row"> 
      <div class="col-md-4"> 
       <input type="checkbox" name="subitem[i]" value="{{$sub->id}}"> {{$sub->subitem_name}} 

      <div class="col-md-4"> 

       <input type="text" name="qty[i]" id="qty" class="form-control" placeholder="Entire Qty"> 
       {!! $errors->first('qty','<p class="help-block">:message</p>') !!} 
      </div> 


      </div> 

      @endforeach 


     </div> 

<div class="form-group"> 
      <input type="submit" class="btn btn-primary" value="Add"> 
     </div> 
     </form> 
    </div> 
    </div> 
</div> 

我需要做的,當我點擊添加按鈕,插入BATCH_ID,sub_id和數量的倍數行.batch_id是相同和數量,sub_id是差異。請問誰能幫我做到這一點?

我還需要改變我的控制器,

public function update(Request $request, $id) 
{ 


    $items = new Itempacking; 

    $items->batch_id     = $request->batch_id; 
    $items->sub_id     = $request->sub_id 
    $items->quantity     = $request->quantity; 

    $items->save(); 
} 

任何人都可以幫助嗎?

+0

嘗試改變'NAME = 「子項[I]」'和'名=「subitem []」'和'qty'也使用你在數組中傳遞數據,所以你需要foreach循環來添加多個數據。 – Nidhi

回答

1

更改你形成這樣

@foreach($subitem as $sub) 

     <div class="row"> 
     <div class="col-md-4"> 
      <input type="checkbox" name="subitem[{{$sub->id}}][id]" value="{{$sub->id}}"> {{$sub->subitem_name}} 

     <div class="col-md-4"> 

      <input type="text" name="subitem[{{$sub->id}}][qty]" value="{{$sub->qty}}}" class="form-control" placeholder="Entire Qty"> 
      {!! $errors->first('qty','<p class="help-block">:message</p>') !!} 
     </div> 


     </div> 

     @endforeach 

迴路元件的$請求 - >分項這樣

` 
     public function update(Request $request, $id) 
     { 

      foreach($request->subitem as $key=>$row) 
      { 

       $items = new Itempacking; 

       $items->batch_id     = $request->batch_id; 
       $items->sub_id     = $row['id'] 
       $items->quantity     = $row['qty']; 

       $items->save(); 

       $items=''; 
     }` 
+0

像這樣顯示錯誤:嘗試獲取非對象的屬性。 $ row is problem – Harshan

+0

改變這兩行$ items-> sub_id = $ row ['id'] $ items-> quantity = $ request ['qty']; – JYoThI

+0

未定義的索引:id ..發生這種情況 – Harshan