Harshan
Harshan

Reputation: 141

Laravel 5.3 - insert data to multiple rows

i am new for laravel.this is my view form

<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>

i need to do, when i click the add button, insert batch_id, sub_id and quantity to multiples rows.batch_id is same and quantity, sub_id are difference.please can anyone help me to do it ?

i need to change my controller also,

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();
}

anyone can help for this ?

Upvotes: 1

Views: 521

Answers (1)

JYoThI
JYoThI

Reputation: 12085

Change you form element like this

 @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

Loop the $request->subitem like this

   `
         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='';
        }`

Upvotes: 1

Related Questions