gpt4 book ai didi

mysql - 动态行未使用 Laravel Controller 保存在数据库中

转载 作者:行者123 更新时间:2023-11-29 15:44:20 25 4
gpt4 key购买 nike

我认为有一张表格要求学生提供教育资格(我提供了下面的代码)。我希望用户(在我的例子中是学生)在填写表格时应该能够动态添加他/她的科目、总分和在数据库表中获得的分数。当我只添加一个主题时,一切正常。但当我尝试添加多个主题时,问题就出现了。期待你们的解决方案..

这是我认为的形式:

<form action="/edu_details" method="post">
@csrf
<div class="jumbotron">
<div class="form-row">
<div class="form-group col-md-12">
<label for="institution">Institution Last Attended</label>
<input type="text" class="form-control" name="institution" id="institution" value="{{old('institution')}}">
@if ($errors->has('institution'))
<div class="error" style="color:red;">{{ $errors->first('institution') }}</div>
@endif
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="yop">Year of Passing</label>
<input type="text" class="form-control" name="yop" id="yop" value="{{old('yop')}}">
@if ($errors->has('yop'))
<div class="error" style="color:red;">{{ $errors->first('yop') }}</div>
@endif
</div>
<div class="form-group col-md-8">
<label for="board">Board</label>
<div class="col-sm-12">
<select name="board" id="board" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>SEBA</option>
<option>CBSE</option>
<option>Other</option>
</select>

</div>
@if ($errors->has('board'))
<div class="error" style="color:red;">{{ $errors->first('board') }}</div>
@endif
</div>
</div>
</div>

<div class="jumbotron">
<table class="table table-bordered">
<thead>
<tr>
<th>Name of the Subject</th>
<th>Total Marks</th>
<th>Marks Obtained</th>
<th><a href="#" class="btn btn-info addRow">+</a></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="subj[]" class="form-control">
@if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
@endif
</td>
<td>
<input type="text" name="totl" class="form-control">
@if ($errors->has('totl'))
<div class="error" style="color:red;">{{ $errors->first('totl') }}</div>
@endif
</td>
<td>
<input type="text" name="obtn" class="form-control">
@if ($errors->has('obtn'))
<div class="error" style="color:red;">{{ $errors->first('obtn') }}</div>
@endif
</td>
<td>
<a href="#" class="btn btn-danger delRow">-</a>
</td>
</tr>
</tbody>
</table>
</div>


<div class="jumbotron">
<div class="form-row">
<label for="sub_group">Group of subjects the applicant wishes to opt :</label>
<div class="form-group col-md-12">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="Major">
<label class="form-check-label" for="sub_group">Major</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="General">
<label class="form-check-label" for="sub_group">General</label>
</div>
</div>
@if ($errors->has('sub_group'))
<div class="error" style="color:red;">{{ $errors->first('sub_group') }}</div>
@endif
</div>
<div class="form-row">

<div class="col-sm-4">
<label for="pref1">1st Preference</label>
<select name="pref1" id="pref1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
@if ($errors->has('pref1'))
<div class="error" style="color:red;">{{ $errors->first('pref1') }}</div>
@endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup1">&nbsp;</label>
<div class="col-sm-12">
<select name="prefgroup1" id="prefgroup1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
@if ($errors->has('prefgroup1'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup1') }}</div>
@endif
</div>
</div>
</div>
<div class="form-row">
<div class="col-sm-4">
<label for="pref2">2nd Preference</label>
<select name="pref2" id="pref2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
@if ($errors->has('pref2'))
<div class="error" style="color:red;">{{ $errors->first('pref2') }}</div>
@endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup2">&nbsp;</label>
<div class="col-sm-12">
<select name="prefgroup2" id="prefgroup2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>

</div>
@if ($errors->has('prefgroup2'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup2') }}</div>
@endif
</div>
</div>
</div>

<button type="submit" class="btn btn-primary btn-block">Submit Data</button>

</form>
</div>
</div>
</div>
</div>
</div>


<script type="text/javascript">
$(".addRow").on('click', function(){
addRow();
return false;
});

function addRow(){
var tr = '<tr>'+
'<td><input type="text" name="subj[]" class="form-control">@if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
@endif </td>'+
'<td><input type="text" name="totl" class="form-control"></td>'+
'<td><input type="text" name="obtn" class="form-control"></td>'+
'<td><a href="#" class="btn btn-danger delRow">-</a></td>'+
'</tr>';
$('tbody').append(tr);

$('tbody').on('click','.delRow',function(){
$(this).parent().parent().remove();
return false;
});
}
</script>

这是我的 Controller 的保存方法:

public function store(Request $request)
{

$validatedData = $request->validate([
'institution' => 'required|max:255',
'yop' => 'required',
'board' => 'required|max:255',
'subj' => 'required|max:255',
'totl' => 'required',
'obtn' => 'required',
'sub_group' => 'required|max:255',
'pref1' => 'required|max:255',
'prefgroup1' => 'required|max:255',
'pref2' => 'required|max:255',
'prefgroup2' => 'required|max:255',
]);

//Insert student data if validated

$edudetail = new EduDetail();
$edudetail->user_id = Auth::user()->id;

//$edudetail->user_id = DB::table('pictures')->where('user_id', $user_id)->value('id');
$edudetail->institution = $request->input('institution');
$edudetail->yop = $request->input('yop');
$edudetail->board = $request->input('board');
foreach($request->get('subj') as $subj) {
$edudetail->subj[] = $subj;
$edudetail->save();
}
$edudetail->totl = $request->input('totl');
$edudetail->obtn = $request->input('obtn');
$edudetail->sub_group = $request->input('sub_group');
$edudetail->pref1 = $request->input('pref1');
$edudetail->prefgroup1 = $request->input('prefgroup1');
$edudetail->pref2 = $request->input('pref2');
$edudetail->prefgroup2 = $request->input('prefgroup2');

$edudetail->save();

return redirect('/student_dox_upload')->with('success','Education Details saved.');

}

我尝试查看是否使用 print_r 方法在主题字段的情况下将值作为数组传递..并且这些值在数组中可见...工作正常..但是当我只保存最后一个时添加动态行的记录..例如,如果我添加 3 个科目英语、数学和科学..我希望将所有 3 个科目添加到一列中,但只添加科学..感谢任何帮助..

最佳答案

像底部一样,为模型内部的输入值创建一个可填充

$fillable=['institution',...];

规则在请求路径上的文件中应用\Http\请求在文件中定义然后将数据保存到下面的表格中

public function store(NewServiceRequest $request,Variable $variable,Variable $variable){
$variable->fill($request->only($variable->getFillable()));
$variable->save();
$variable->variable()->save($variable);
.
.
.
}

关于mysql - 动态行未使用 Laravel Controller 保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240236/

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