Laravel - 节省动态输入数据&排列

时间:2017-11-21 21:01:40

标签: php arrays laravel post laravel-5

我正在处理一个我正在研究的小项目中相当复杂的部分。

我已收到包含发货项目的发票(在型号上标注为发票和invoice_item),并且货件通常会有多个发票项目。以下是当前货件项目部分的示例 -

enter image description here

当然它只从一行开始(一行带有"最后添加更多"按钮)。使用货运控制器(右下方),我可以轻松地将货件_detail保存到我正在处理的特定货件中。

货件控制器

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');

    }

但是,我的问题是我无法一次向cargo_details保存多条记录。我知道这必须有一个输出作为数组,但我不确定如何为这个特殊情况做到这一点。有没有人遇到过这样的事情?

PS

shipping_details部分的表单如下:

<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 个答案:

答案 0 :(得分:1)

在输入类型上使用数组表示法, 例如:<input type="text" name="some_name[]">

使用for循环获取所有值并保存