Laravel 5.3 - 将数据插入多行

时间:2017-05-17 03:55:36

标签: php laravel laravel-5.3

我是laravel的新手。这是我的观点

<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和quantity插入到倍数rows.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();
}

任何人都可以为此提供帮助吗?

1 个答案:

答案 0 :(得分: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

循环 $ request-&gt;子项目,就像这样

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