【问题标题】:Updating child record while updating parent in Laravel在 Laravel 中更新父级时更新子级记录
【发布时间】:2018-01-12 17:59:53
【问题描述】:

我有一个“装运”模型和一个“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"

【问题讨论】:

  • shipment_details 记录是否已经存在,您只是想更新它还是想创建一个新的shipment_details 记录?
  • 目前只是更新已经存在的shipment_details
  • 根据我最近对您之前的一个问题所做的说明:在撰写您的问题时,马修,您会不会让他们少说些话?我们在这里更喜欢简洁的问题。预先感谢、感谢信、签名和各种其他项目(问候、问候、希望有人能提供帮助、截止日期等)往往会被删减,如果一开始没有添加,志愿编辑将非常感激.

标签: php laravel laravel-5 model controller


【解决方案1】:

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

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

{{Form::select('shipment_details['.$sd-&gt;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 循环。

【讨论】:

  • 好吧,我已经实施了您的建议,但我收到以下错误:尝试在此行获取非对象的属性:$shipdetail-&gt;pieces_type = $details-&gt;piecesType; -- 我将使用我的完整代码更新我的问题现在正在使用,因此它不会填充此评论。
  • 这意味着$shipdetail = Shipment_Details::find($id); 正在返回null;添加支票:if($shipDetail){ ... }.
  • 我会补充一点,但我实际上是在一个确实附加有 shipping_detail 的设备上进行测试的。
  • 我刚刚注意到这个问题并在我的回答中修复了它。它是 $details-&gt;piecesType; $details 是一个数组而不是一个对象,所以它应该是 $details['piecesType']。但是蒂姆的评论也非常有效,无论如何都应该这样做以避免在尝试$shipdetail-&gt;someProperty时抛出错误。
  • 啊,不错,不错。我读错了(可能是由于类似的变量名称),但是是的,$shipdetail 的属性可以通过-&gt; 访问,除非null$details 作为一个数组,需要数组语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
相关资源
最近更新 更多