在Laravel中更新父级时更新子记录

时间:2018-01-12 17:59:54

标签: php laravel laravel-5 model controller

我有一个“货件”的模型和“shipment_details”的子模型,我创建了一个编辑表单,如果我去编辑整个货件,我希望能够进行更新也是shipment_details

目前,这是我的“发货”型号:

public function shipment_details(){
    return $this->hasMany('App\Shipment_Detail', 'shipment_id','pro_number');
}

这是我的编辑页面更新的控制器:

public function updateRecord(Request $request, $id)
    {

      $shipment = Shipment::where('UUID','=',$id)->firstOrFail();

        //Save Initial Shipment Data
        $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 = 1;
        $shipment->shipment_billing_status = (isset($request->shipment_billing_status) && !empty($request->shipment_billing_status)) ? $request->shipment_billing_status : 1;
        $shipment->cn_billtoName = request('cn_billtoName');
        $shipment->cn_billtoAddress1 = request('cn_billtoAddress1');
        $shipment->cn_billtoAddress2 = request('cn_billtoAddress2');
        $shipment->cn_billtoCity = request('cn_billtoCity');
        $shipment->cn_billtoState = request('cn_billtoState');
        $shipment->cn_billtoZip = request('cn_billtoZip');
        $shipment->cn_billtoEmail = request('cn_billtoEmail');
        $shipment->cn_billtoPhone = request('cn_billtoPhone');
        $shipment->cn_shiptoName = request('cn_shiptoName');
        $shipment->cn_shiptoAddress1 = request('cn_shiptoAddress1');
        $shipment->cn_shiptoAddress2 = request('cn_shiptoAddress2');
        $shipment->cn_shiptoCity = request('cn_shiptoCity');
        $shipment->cn_shiptoState = request('cn_shiptoState');
        $shipment->cn_shiptoZip = request('cn_shiptoZip');
        $shipment->cn_shiptoEmail = request('cn_shiptoEmail');
        $shipment->cn_shiptoPhone = request('cn_shiptoPhone');
        $shipment->cn_shipfromName = request('cn_shipfromName');
        $shipment->cn_shipfromAddress1 = request('cn_shipfromAddress1');
        $shipment->cn_shipfromAddress2 = request('cn_shipfromAddress2');
        $shipment->cn_shipfromCity = request('cn_shipfromCity');
        $shipment->cn_shipfromState = request('cn_shipfromState');
        $shipment->cn_shipfromZip = request('cn_shipfromZip');
        $shipment->cn_shipfromEmail = request('cn_shipfromEmail');
        $shipment->cn_shipfromPhone = request('cn_shipfromPhone');
        $shipment->fuelChargeDesc = request('fuelChargeDesc');
        $shipment->fuelChargeAmt = request('fuelChargeAmt');
        $shipment->fuelChargeTotal = request('fuelChargeTotal');
        $shipment->permitChargeDesc = request('permitChargeDesc');
        $shipment->permitChargeAmt = request('permitChargeAmt');
        $shipment->permitChargeTotal = request('permitChargeTotal');
        $shipment->otherChargeDesc = request('otherChargeDesc');
        $shipment->otherChargeAmt = request('otherChargeAmt');
        $shipment->otherChargeTotal = request('otherChargeTotal');
        $shipment->noCharge = request('noCharge');
        $shipment->noSettle = request('noSettle');
        $shipment->Total = request('Total');
        if ((request('shipment_billing_status') == 2) || (request('shipment_billing_status') == 3)){
           $balance = 0.00;
        }else{
           $balance = request('Total');
        }
        $shipment->Balance = $balance;
        $shipment->freightBillSubtotal = request('freightBillSubtotal');

        $shipment->save();

        //Save Shipment Details -- NEED HELP HERE //

           for ($i = 0; $i < count($request->shipment_details['piecesNumber']); $i++) {

            $Shipment_Detail = Shipment_Detail::where
            Shipment_Detail::create([
                'shipment_id' => $shipment->pro_number,
                'pieces_number' => $request->shipment_details['piecesNumber'][$i],
                'pieces_type' => $request->shipment_details['piecesType'][$i],
                'rate_type' => $request->shipment_details['rateType'][$i],
                'charge' => $request->shipment_details['charge'][$i],
                'weight' => $request->shipment_details['weight'][$i],
                'hazmat' => $request->shipment_details['hazmat'][$i],
                'description' => $request->shipment_details['description'][$i] ]);
        }

        //END HELP HERE SECTION//


        Session::flash('success_message','Freight Bill Successfully Updated'); //<--FLASH MESSAGE

        //Return to Register//
        return redirect('/shipments/i/'.$shipment->UUID);

    }

在底部附近,您可以看到我已经标出了需要帮助的部分的部分。那里的代码部分取自我的存储创建页面,并且运行良好。我假设我可以在这种情况下使用它,但我不确定如何识别要更新的shipment_details以及如何将其传递给shipment_details数据库表记录中的更新。

这是我特定于与正在更新的货件相关联的shipment_details的HTML:

  <tbody>
                    @foreach($shipment_details as $sd)
                        <tr style="height:40px">
                            <td style="width:8%;text-align:center;">{{Form::text('shipment_details[piecesNumber][]', $sd->pieces_number, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}
                            </td>
                            <td style="width:16%;text-align:center;">
                            {!! Form::select('shipment_details[piecesType][]', $piecetypes, $sd->pieces_type, ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!}    
                            </td>
                            <td>
                            {!! Form::select('shipment_details[rateType][]', $ratetypes, $sd->rate_type, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details[weight][]', $sd->weight, array('class' => 'form-control','placeholder'=>'Weight','required','id'=>'weight'))}}    
                            </td>
                            <td style="width:16.5%;text-align:center;">
                             {{Form::select('shipment_details[hazmat][]',array(
                                    'No'=>'No',
                                    'Yes'=>'Yes',
                                ), $sd->hazmat, array('class' => 'form-control','id'=>'hazmat'))}}   
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details[description][]', $sd->description, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}} 
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details[charge][]', $sd->charge, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}} 
                            </td>
                            <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
                </tr> 
                    @endforeach
            </tbody>

使用以下内容将shipment_details通过控制器拉到页面:

$shipment_details = $shipment->shipment_details;

响应回答的更新

我现在使用以下内容在我的shipment控制器中保存详细信息:

        foreach ( $request->shipment_details as $id => $details ) {
            $shipdetail = Shipment_Detail::find($id);
            $shipdetail->pieces_type = $details->piecesType;
            $shipdetail->pieces_number = $details->piecesNumber;
            $shipdetail->rate_type = $details->rateType;
            $shipdetail->weight = $details->weight;
            $shipdetail->charge = $details->charge;
            $shipdetail->description = $details->description;
            $shipdetail->hazmat = $details->hazmat;
            // Other info to update here
            $shipdetail->save();
        }

我的html表单:

@foreach($shipment_details as $sd)
                        <tr style="height:40px">
                            <td style="width:8%;text-align:center;">{{Form::text('shipment_details['.$sd->id.'][piecesNumber]', $sd->pieces_number, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}
                            </td>
                            <td style="width:16%;text-align:center;">
                            {!! Form::select('shipment_details['.$sd->id.'][piecesType]', $piecetypes, $sd->pieces_type, ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!}    
                            </td>
                            <td>
                            {!! Form::select('shipment_details['.$sd->id.'][rateType]', $ratetypes, $sd->rate_type, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details['.$sd->id.'][weight]', $sd->weight, array('class' => 'form-control','placeholder'=>'Weight','required','id'=>'weight'))}}    
                            </td>
                            <td style="width:16.5%;text-align:center;">
                             {{Form::select('shipment_details['.$sd->id.'][hazmat]',array(
                                    'No'=>'No',
                                    'Yes'=>'Yes',
                                ), $sd->hazmat, array('class' => 'form-control','id'=>'hazmat'))}}   
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details['.$sd->id.'][description]', $sd->description, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}} 
                            </td>
                            <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details['.$sd->id.'][charge]', $sd->charge, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}} 
                            </td>
                            <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
                </tr>

                    @endforeach

我会提到这个数组在我的错误页面中,所以我知道细节实际上已被传递:

shipment_billing_status     

""

shipment_details    

array:1 [▼
  13032 => array:7 [▼
    "piecesNumber" => "1"
    "piecesType" => "1"
    "rateType" => "1"
    "weight" => "454"
    "hazmat" => "No"
    "description" => "LARGE CLAM - L"
    "charge" => "65.00"
  ]
]

freightBillSubtotal     

"65.00"

1 个答案:

答案 0 :(得分:2)

由于记录已经存在,我们可以稍微修改您的HTML,以便在HTML中获取正确的ID以传递给您的控制器。您需要从

更改每个现有输入

{{Form::select('shipment_details[piecesType][]', ..)}}

{{Form::select('shipment_details['.$sd->id.']["piecesType"], ..)}}

在控制器中,这给我们一个例子:
shipment_details[2]['piecesType'] // which equals some value

现在,我们可以使用控制器:

foreach ( $request->shipment_details as $id => $details ) {
    $shipdetail = Shipment_Details::find($id);
    $shipdetail->piecesType = $details['piecesType'];
    // Other info to update here
    $shipdetail->save();
}

这将取代您当前的for循环。