Php 更新Laravel中的父记录时更新子记录
我有一个“装运”模型和一个子模型“Php 更新Laravel中的父记录时更新子记录,php,laravel,laravel-5,model,controller,Php,Laravel,Laravel 5,Model,Controller,我有一个“装运”模型和一个子模型“shipping\u details”,我创建了一个编辑表单,如果我要编辑整个装运,我希望能够对装运\u details进行更新 目前,这是我的“装运”模型: 这是我的编辑页面更新控制器: public function updateRecord(Request $request, $id) { $shipment = Shipment::where('UUID','=',$id)->firstOrFail(); /
shipping\u details
”,我创建了一个编辑表单,如果我要编辑整个装运,我希望能够对装运\u details
进行更新
目前,这是我的“装运”模型:
这是我的编辑页面更新控制器:
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->shipment_details;
更新对答案的回应
我现在使用以下命令将详细信息保存在我的装运
控制器中:
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
由于记录已经存在,我们可以稍微修改您的HTML,将适当的ID放入HTML中,以传递给控制器。您的每个现有输入都需要从
{{Form::select('shipping_details[piecesType][],…)}
到
{Form::select('shipping_details['.$sd->id.][“piecesType”],…)}
在控制器中,这为我们提供了一个示例:shipping\u详细信息[2]['piecesType']//等于某个值
现在,对于控制器,我们可以使用:
foreach ( $request->shipment_details as $id => $details ) {
$shipdetail = Shipment_Details::find($id);
$shipdetail->piecesType = $details['piecesType'];
// Other info to update here
$shipdetail->save();
}
它将取代当前的
for
循环。由于记录已经存在,我们可以稍微修改HTML,将正确的ID放入HTML中,以传递给控制器。您的每个现有输入都需要从
{{Form::select('shipping_details[piecesType][],…)}
到
{Form::select('shipping_details['.$sd->id.][“piecesType”],…)}
在控制器中,这为我们提供了一个示例:shipping\u详细信息[2]['piecesType']//等于某个值
现在,对于控制器,我们可以使用:
foreach ( $request->shipment_details as $id => $details ) {
$shipdetail = Shipment_Details::find($id);
$shipdetail->piecesType = $details['piecesType'];
// Other info to update here
$shipdetail->save();
}
这将替换当前的
for
循环。是否已经存在装运详细信息
记录,您只是想更新它,还是想创建一个新的装运详细信息
记录,这只是更新已经存在的装运详细信息。正如我最近在你之前的一个问题上所做的说明:在撰写你的问题时,Matthew,你会让它们少一点闲聊吗?我们更喜欢简洁的问题。预先致谢、感谢信、签名和其他各种物品(问候、问候、希望有人能帮忙、截止日期等)往往会被删减,志愿者编辑非常感谢,如果他们没有被添加到第一位。是否已经存在装运详细信息
记录,您只是想更新它,还是想创建一个新的装运详细信息
记录,这只是更新已经存在的装运详细信息。正如我最近在你之前的一个问题上所做的说明:在撰写你的问题时,Matthew,你会让它们少一点闲聊吗?我们更喜欢简洁的问题。预先致谢、感谢信、签名和其他各种物品(问候、问候、希望有人能帮忙、截止日期等)往往会被删减,如果没有添加,志愿者编辑将非常感激。好吧,我已经实现了您的建议,但我得到了以下错误:尝试获取此行非对象的属性:$shipdetail->pieces\u type=$details->piecesType代码>--我将用我现在使用的完整代码更新我的问题,这样它就不会塞满这个评论。这意味着$shipdetail=shipping\u Details::find($id)
正在返回null
;添加一个检查:if($shipDetail){…}
。我会添加它,但实际上我在一个附加了装运详细信息的机器上测试了它。我刚刚注意到这个问题,并在我的回答中修复了它。它是$details->piecesType
$details
是一个数组而不是一个对象,因此它应该是$details['piecesType']
。但是Tim的评论也是非常有效的,不管怎么做都应该避免在尝试$shipdetail->someProperty
时抛出错误。啊,是的,很好。我读错了(可能是因为类似的变量名),但是是的,$shipdetail
的属性可以通过->
访问,除非null
和$details
是一个数组,需要数组语法。好的,我已经实现了你的建议,但是我得到了以下错误:尝试获取此行非对象的属性:$shipdetail->pieces\u type=$details->piecesType代码>--我将用我现在使用的完整代码更新我的问题,这样它就不会塞满这个评论。这意味着$shipdetail=shipping\u Details::find($id)
正在返回null
;添加一个检查:if($shipDetail){…}
。我会添加它,但实际上我在一个附加了装运详细信息的机器上测试了它。我刚刚注意到这个问题,并在我的回答中修复了它。它是$details->piecesType
$details
是一个数组而不是一个对象,因此它应该是$details['piecesType']
。但是Tim的评论也是非常有效的,不管怎么做都应该避免在尝试$shipdetail->someProperty
时抛出错误。啊,是的,很好。我读错了(可能是因为类似的变量名),但是是的,$shipdetail
的属性可以通过->
访问,除非null
,并且$details
作为一个数组,需要数组语法。
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"
foreach ( $request->shipment_details as $id => $details ) {
$shipdetail = Shipment_Details::find($id);
$shipdetail->piecesType = $details['piecesType'];
// Other info to update here
$shipdetail->save();
}