gpt4 book ai didi

php - 在 Laravel 中对多个子记录使用 createOrUpdate

转载 作者:可可西里 更新时间:2023-10-31 23:36:26 30 4
gpt4 key购买 nike

我有一个“shipment”模型,其中有很多“shipment_details”模型记录。

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

当我创建“shipment”记录时,最初创建“shipment_details”没有问题。

当我想更新“shipment”记录并且最终需要在下面添加额外的“shipment_detail”记录时,我的问题就出现了。

目前我的“shipment_details”的 html 是来自 Blade 的:

<tbody>
@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(
'0'=>'No',
'1'=>'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
</tbody>

添加更多按钮是指这个脚本:

<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#freight_bill_items').append('<tr id="row'+i+'"> <td style="width:8%;text-align:center;">{{Form::text('shipment_details[piecesNumber][]', null, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}</td><td style="width:16%;text-align:center;">{!! Form::select('shipment_details[piecesType][]', $piecetypes, 'null', ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!} </td><td>{!! Form::select('shipment_details[rateType][]', $ratetypes, null, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[weight][]', null, 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',), null, array('class' => 'form-control','id'=>'hazmat'))}}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[description][]', null, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[charge][]', null, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}}</td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
$( 'input[name^="shipment_details[charge][]"]' ).trigger( "change" );
});

});
</script>

然后在当前时刻,我的“shipment_details”通过 Controller 以下列方式“保存”:

    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();
}

所以我的问题是,有没有办法增加“shipment_details”?它需要使用 updateOrCreate 之类的东西还是需要其他东西?

更新

这就是我的数组目前的发布方式,目前发布时出现错误 - undefined index :piecesType。目前,如果我更新当前的 shipment_detail,它工作正常,添加新的会产生问题。

shipment_details    

array:8 [▼
13149 => array:7 [▼
"piecesNumber" => "1"
"piecesType" => "1"
"rateType" => "1"
"weight" => "12"
"hazmat" => "0"
"description" => "124"
"charge" => "12.00"
]
"piecesNumber" => array:1 [▼
0 => "3"
]
"piecesType" => array:1 [▼
0 => "2"
]
"rateType" => array:1 [▼
0 => "2"
]
"weight" => array:1 [▼
0 => "1200"
]
"hazmat" => array:1 [▼
0 => "Yes"
]
"description" => array:1 [▼
0 => "desc2"
]
"charge" => array:1 [▼
0 => "40.00"
]
]

更新

ALEXEY 的装运 Controller

foreach ($request->shipment_details as $id => $details) {
$shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
'pieces_type' => $details['piecesType'][0],
'pieces_number' => $details['piecesNumber'][0],
'rate_type' => $details['rateType'][0],
'weight' => $details['weight'][0],
'charge' => $details['charge'][0],
'description' => $details['description'][0],
'hazmat' => $details['hazmat'][0]
]);
}

更新

更新为 dd($id, $details);

13149

array:7 [▼
"piecesNumber" => "1"
"piecesType" => "1"
"rateType" => "1"
"weight" => "12"
"hazmat" => "0"
"description" => "124"
"charge" => "12.00"
]

但它不包括我试图通过此保存添加的最近添加的行。

最佳答案

您可以像这样使用 updateOrCreate():

foreach ($request->shipment_details as $id => $details) {
$shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
'pieces_type' => $details['piecesType'],
'pieces_number' => $details['piecesNumber'],
'rate_type' => $details['rateType'],
'weight' => $details['weight'],
'charge' => $details['charge'],
'description' => $details['description'],
'hazmat' => $details['hazmat']
]);
}

关于php - 在 Laravel 中对多个子记录使用 createOrUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48456150/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com