【发布时间】:2018-08-23 05:02:48
【问题描述】:
我有一个页面,允许编辑会议注册的注册表单。在此表单中,有一个表格显示该会议存在的问题,然后用户可以通过复选框将每个问题与注册类型相关联。
选中复选框后,他可以点击更新按钮更新注册表。
怀疑:
我的疑问是在用户单击“更新”按钮后如何更新注册表单,同时考虑到选中的复选框。复选框 id 和 value 是动态的,存储在注册表类型表中。那么如何在更新方法中获取选中的复选框,以便可以根据选中的复选框更新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>
【问题讨论】: