我有一个"货物"模型有很多" shipment_details"模型记录。
public function shipment_details(){
return $this->hasMany('App\Shipment_Detail', 'shipmentID','id');
}
我最初创建" shipment_details"当我创建一个"货物"记录。
当我想要更新"货物时,我的问题就出现了。记录,我最终需要添加额外的" shipment_detail"在下面记录。
目前我的html of" shipment_details"就像刀片一样:
<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>
然后在当前时刻,我的&#34; shipment_details&#34;被&#34;保存&#34;通过以下方式通过控制器:
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();
}
所以我的问题是,是否有办法增加&#34; shipment_details&#34;?它需要使用像updateOrCreate这样的东西还是需要别的东西?
这就是我的数组目前发布的方式,目前正在发布错误 - 未定义索引: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的货件控制器
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"
]
但它并没有包含我尝试使用此保存添加的最近添加的行。
答案 0 :(得分:1)
您可以像这样使用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']
]);
}