我正在使用laravel进行项目。该站点的一项功能是将动态输入中的数据保存为四个数据。 应该保存到数据库的MObjective,MOutput,年份和kra_id
我尝试使用此代码进行保存; ''' $ objectives = $ request-> all();
foreach ($objectives as $key) {
// dd($input);
$input = objective::create(array(
'MObjective' => $objectives->MObjective[$key],
'MOutput' => $objectives->MOutput[$key],
'year' => $objectives->year,
'kra_id' => $objectives->kra_id
));
}
''' 我也尝试使用for循环,但是出现了一些错误,例如;试图获取非对象的属性“ MObjective”
这是五月控制器;
$objectives = $request->all();
foreach ($objectives as $key) {
// dd($input);
$input = objective::create(array(
'MObjective' => $objectives->MObjective[$key],
'MOutput' => $objectives->MOutput[$key],
'year' => $objectives->year,
'kra_id' => $objectives->kra_id
));
}
我的刀片文件;
<form method="POST" action="{{action('ObjectiveController@store')}}">
{{csrf_field()}}
<div class="form-group">
<input type="hidden" name="year" id="year" value="{{now()->year}}" class="form-control">
<input type="hidden" class="form-control" id="kra_id" name="kra_id">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Major Objective:</label>
<textarea class="form-control" id="MObjective" name="MObjective"></textarea>
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Major Output:</label>
<textarea class="form-control" id="MajorOutput" name="MOutput"></textarea>
</div>
<div class="modal-footer">
<b><i>Note:</i></b>
<small>Add Major Objective and Output for selected KRA only</small>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Add</button>
</div>
</form>
我的剧本 '''
var i=0;
$('.addRow').on('click', function(){
i++;
addRow();
});
function addRow()
{
var tr='<div class="row">'+'<div class="col-6"><textarea type="text" class="form-control" placeholder="Enter the Major Objective no. '+i+' "name="MObjective['+i+']"></textarea></div>'+
'<div class="col-6"><textarea type="text" class="form-control" name="MOutput['+i+']" placeholder="Enter Major Output for the objective no. '+i+'"></textarea></div>'+
'</div><br>';
$('form').append(tr);
}
'''
我只需要将4个数据保存在一个db表中; MObjective,MOutput,年份和kra_id。我希望有人可以帮助我。预先谢谢你
答案 0 :(得分:0)
在您的控制器中,如果没有记错,$ objectives应该是一个数组,而不是一个对象。