我有一个输入标记,该输入标记正在表单中用id解析。我正在尝试获取 $data = $request->all();
我试图提取该值并将其存储在[licensing-1-61]中显示。问题是我不确定在保存值时如何解析id:
刀片:
<input type="text" value="{{isset($two->answer->score) ? $two->answer->score : '' }}" name="licensing-{{ $one->id.'-'.$two->id }}" id="licensing-{{ $one->id.'-'.$two->id }}" style="min-width: 100px;" onblur="saveScore()"/>
Ajax函数:
function saveScore()
{
var datastring = $("#score-license").serialize();
if(datastring != '')
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url:"{{route('staff.regulatory.licensing.storeScore',['id'=>$two->id])}}",
type: 'PUT',
data: datastring,
dataType : 'JSON',
success:function(data)
{
console.log(data);
alert("Success");
},
error:function(message)
{
//location.reload();
alert(JSON.stringify(message));
}
});
}
}
控制器:
public function storeScore(Request $request, $id)
{
//print_r($request->all());
$data = $request->all();
$selected = PreLicensingMain::where('id', $id)
->with('levelone.leveltwo')
->first();
$selected->main_answer = PreLicensingMainAnswer::where('pre_licensing_main_id', $selected->id)->where('centre_id', $staff->centre_id)->first();
foreach ($selected->levelone as $key => $one) {
foreach ($one->leveltwo as $key => $two) {
$two->answer = PreLicensingAnswer::where('pre_licensing_level_two_id', $two->id)->where('centre_id', $staff->centre_id)->first();
}
}
foreach ($data["licensing-{{ $one->id.'-'.$two->id }}"] as $group) {
if (!is_null($group["licensing"])) {
$licensingtask = new PreLicensingAnswer;
$licensingtask->score = $group["licensing"];
$licensingtask->save();
}
}
}