我有一个页面允许编辑会议注册的注册表单。在此表单中,有一个表格,显示该会议存在的问题,然后用户可以将每个问题与注册类型通过复选框相关联。
选中复选框后,他可以点击更新按钮更新注册表。
的疑问:
我的疑问是在用户点击"更新"按钮如何更新注册表单,同时考虑所选的复选框。复选框id和value是动态的,存储在注册类型表中。那么如何在update方法中获取选中的复选框,以便根据所选复选框更新registration_type_questions数据透视表?
问题控制器:
// returns the view to edit the conference registration form
public function edit($id)
{
$conference = Conference::find($id);
$registrationType = RegistrationType::where('conference_id', $id)->get();
$question = Question::where('conference_id', $id)->get();
return view('questions.edit')
->with('conference', $conference)
->with('registration_type', $registrationType)
->with('question', $question);
}
}
// update the registration form based on checkboxes selected
public function update(Request $request, $id){
$this->validate($request, [
// ??
]);
// ??
}
RegistrationType模型:
class RegistrationType extends Model
{
protected $fillable = [
'name','conference_id',...
];
public function conference(){
return $this->belongsTo('App\Conference');
}
public function questions(){
return $this->belongsToMany('App\Question', 'registration_type_questions');
}
}
问题模型:
class Question extends Model
{
protected $fillable = [
'question', 'type', 'conference_id',
];
public function registration_type(){
return $this->belongsToMany('App\RegistrationType', 'registration_type_questions');
}
}
修改会议注册表单代码:
<form id="edit_registration_types" method="post" class="clearfix" action="{{route('rtypes.update', ['conf_id' => $conf->id])}}">
{{csrf_field()}}
<div class="form-row">
<div class="form-group col">
<div class="row">
<div class="col">
<table class="table table-striped table-responsive-sm">
<thead>
<tr>
<th scope="col">Info</th>
<th scope="col">Include in registration type</th>
<th scope="col">Mandatory Field</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>
<i class='fa fa-check font-size-lg text-primary ml-4'></i>
</td>
<td>
<i class='fa fa-check font-size-lg text-primary ml-4'></i>
</td>
</tr>
<tr>
<td>Surname</td>
<td>
<i class='fa fa-check font-size-lg text-primary ml-4'></i>
</td>
<td>
<i class='fa fa-check font-size-lg text-primary ml-4'></i>
</td>
</tr>
@foreach($question as $q)
<tr>
<td>{{$q->question}}</td>
<td>
@foreach($registration_type as $rtype)
<div class="form-check">
<input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}}
</label>
</div>
@endforeach
</td>
<td>
@foreach($registration_type as $rtype)
<div class="form-check">
<input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
<label class="form-check-label" for="exampleRadios1">
for the registration type "{{$rtype->name}}"
</label>
</div>
@endforeach
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<input type="submit" class="btn btn-primary float-right mt-3" value="Update"/>
</form>
答案 0 :(得分:0)
我理解的很多,你只想要一种简单的方法来从前端捕获动态复选框信息。如果是这样,那么你可以简单地使用为此构建的html命名约定。例如mycheckbox[]
将此作为所有复选框名称使用,并处理后端的值,即
@foreach($registration_type as $rtype)
<div class="form-check">
<input name="mycheckbox[]" autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}}
</label>
</div>
@endforeach
后端:
// update the registration form based on checkboxes selected
public function update(Request $request, $id){
$values = $request->get('mycheckbox');
foreach($values as $rtype_id){
//do whatever you want with this rtype id that was checked