dpoh61610 2017-11-21 21:01 采纳率: 100%
浏览 75
已采纳

Laravel - 保存动态输入数据和数组

I'm working on a rather complex portion of a little project I'm working on.

I've got an invoice with shipment items (labeled on models as invoice and invoice_item), and often times an shipment will have multiple invoice items. Below is an example of the current shipment item section -

enter image description here

Of course it starts off with only one row (the one with the "Add more" button at the end). And using the shipment controller (right below), I can easily save an shipment_detail to that specific shipment that I am working on.

Shipment Controller

public function store(Shipment $request)
    {

        $this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required'

        ]);

        $user_id = Auth::id();

        $input = $request->all();

        $shipment = new Shipment();
        $shipment->pro_number = request('pro_number');
        $shipment->shipment_origin = request('shipment_origin');
        $shipment->date = request('date');
        $shipment->due_date = request('due_date');
        $shipment->tractor_id = request('tractor_id');
        $shipment->trailer_id = request('trailer_id');
        $shipment->driver_id = request('driver_id');
        $shipment->notes = request('notes');
        $shipment->shipper_no = request('shipper_no');
        $shipment->ship_to = request('ship_to');
        $shipment->ship_from = request('ship_from');
        $shipment->bill_to = request('bill_to');
        $shipment->bill_type = request('bill_type');
        $shipment->load_date = request('load_date');
        $shipment->shipment_status = request('shipment_status');
        $shipment->created_by = $user_id;

        $shipment_detail = new Shipment_Detail();
        $shipment_detail->pieces_number = request('pieces_number');
        $shipment_detail->pieces_type = request('pieces_type');
        $shipment_detail->rate_type = request('rate_type');
        $shipment_detail->weight = request('weight');
        $shipment_detail->hazmat = request('hazmat');
        $shipment_detail->description = request('description');
        $shipment_detail->charge = request('charge');

        $shipment->save();
        $shipment_detail->shipment_id = $shipment->id;
        $shipment->shipment_details()->save($shipment_detail);

        return redirect('/shipments');

    }

However, my problem here is that I cannot save more than one record at a time to the shipment_details. I know this has to have an output as an array but I am unsure how to do that for this particular case. Has anyone run across something like this before?

PS

My form for the shipment_details section looks as such:

<table style="width:100%;" id="freight_bill_items">
                <thead>

                    <td style="width:8%;font-weight:bold;text-align:center;">No. Pieces</td><td style="width:8%; font-weight:bold; text-align:center;">Pieces Type</td><td style="width:16.5%;font-weight:bold;text-align:center;">Rate Type</td><td style="width:16.5%;font-weight:bold;text-align:center;">Weight(lbs)</td><td style="width:16.5%;font-weight:bold;text-align:center;color:red;">HazMat?</td><td style="width:16.5%;font-weight:bold;text-align:center;">Description</td><td style="width:16.5%;font-weight:bold;text-align:center;">Charge</td><td></td>

                </thead>
                <tbody>
                    <tr style="height:40px">
                <td style="width:8%;text-align:center;"><input type="text" name="pieces_number" class="form-control name_list" id="pieces_number" placeholder="No. Pieces"></td><td style="width:16%;text-align:center;"><select style="width:100%; display:inline" class="form-control" name="pieces_type" id="pieces_type">
                    <option selected value="">Please select</option>
     @foreach($piecetypes as $piecetype)
     <option value="{{ $piecetype->id }}">{{ $piecetype->label }}</option>
     @endforeach
                    </select></td><td><select style="width:100%; display:inline" class="form-control" name="rate_type" id="rate_type">
                    <option selected value="">Please select</option>
     @foreach($ratetypes as $ratetype)
     <option value="{{ $ratetype->id }}">{{ $ratetype->label }}</option>
     @endforeach
                    </select></td><td style="width:16.5%;text-align:center;"><input type="text" name="weight" id="weight" class="form-control name_list" placeholder="Weight"></td><td style="width:16.5%;text-align:center;"><select style="width:100%; display:inline" class="form-control" name="hazmat" id="hazmat">
                    <option selected value="0">No</option>
                        <option>Yes</option>
                    </select></td><td style="width:16.5%;text-align:center;"><input type="text" name="description" class="form-control name_list" placeholder="Description" id="description"></td><td style="width:16.5%;text-align:center;"><input type="text" name="charge" class="form-control name_list" placeholder="Charge" id="charge"></td<td><td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
                </tr>
            </tbody>
            </table>
  • 写回答

1条回答 默认 最新

  • dongqiao1158 2017-11-22 12:23
    关注

    use array notation on input types, eg: <input type="text" name="some_name[]">

    use a for loop to get all the values and save it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站