0

I am trying to add multiple subitems to my eloquent model as arrays but am unable to do so.

View:

  <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="product_id" id="product_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('item_id')) ? $errors->first('title') : '' }}">
          <lable>Item Name</lable>

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

          <input type="text" class="form-control" value="{{$rm->item->item_name}}" readonly="">
          {!! $errors->first('wastage','<p class="help-block">:message</p>') !!}
        </div>

        <div class="form-group{{ ($errors->has('rquantity')) ? $errors->first('title') : '' }}">
          <lable>Quantity (Kg)</lable>
          <input type="text" name="rquantity" id="rquantity" class="form-control" value="{{$rm->quantity}}" 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[{{$sub->id}}]['subitem_id']" value="{{$sub->id}}">  {{$sub->subitem_name}}<br><br>

            </div>

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

              <input type="text" name="subitem[{{$sub->id}}]['qty']" 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>

Controller:

public function update(Request $request, $id)
{
    $result = Production::findOrFail($id);

    foreach($request->subitem as $key=>$row)
    {
        //print_r($request->subitem);exit;

        $items = new Itempacking;

        $items->batch_id          = $result->product_id;
        $items->item_id           = $result->item_id;
        $items->rquantity         = $result->quantity;
        $items->product_id        = $result->id;

        $items->subitem_id        = $row['subitem_id'];
        $items->qty               = $row['qty'];
        $items->status            = 1;  

        $items->save();

        $items='';
    }
}

When I uncomment this line print_r($request->subitem); exit; I can see like this:

Array ( 
    [3] => Array ( 
        ['subitem_id'] => 3 
        ['qty'] => 2 
    ) 
    [4] => Array ( 
        ['subitem_id'] => 4 
        ['qty'] => 3 
    )
)

But when I comment and try to send data to table it is not working. It gives this message

Undefined index: subitem_id

9
  • If you look, you have an array of subitems, so you'll need to decide the best way you want to put multiple subitems into your tables. Commented May 17, 2017 at 14:25
  • thank you..can you help me build update function in controller ? Commented May 17, 2017 at 14:27
  • No. As I said, you'll need to decide how best to add multiple subitems. Commented May 17, 2017 at 14:28
  • please let me know , my function is wrong..i remove $row['subitem_id'], $row['qty'] and put $items->subitem_id= 1; $items->qty = 2;..then it is work..but $row['subitem_id'] not work Commented May 17, 2017 at 14:32
  • you will need to use a foreach loop Commented May 17, 2017 at 14:45

1 Answer 1

1

Don't insert '' for key of array in HTML code.You can print your request to details.

Replace from

subitem[{{$sub->id}}]['subitem_id']
subitem[{{$sub->id}}]['qty']

to

subitem[{{$sub->id}}][subitem_id]
subitem[{{$sub->id}}][qty]
Sign up to request clarification or add additional context in comments.

3 Comments

i tried it..but result is same thing..Undefined index: subitem_id
$arr = array( 'subitem_id'=>3, 'qty'=>2, ); print_r($arr); run this code and compare with your result.
same results come...but still same error happen..Undefined index: subitem_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.