【发布时间】:2018-09-01 14:23:14
【问题描述】:
我有一个包含一些问题/字段(电子邮件、电话号码等)的表单,然后用户可以选择是否应为会议存在的注册类型包含该字段,以及该字段是否应通过复选框对每个注册类型强制或不强制。
根据选中的复选框,将信息插入到具有以下结构的数据透视表“registration_type_questions”中:registration_type_id、question_id、必需。
数据库暂时是这样的:
registration_type_id question_id required
2 1 1 (include quetion 1 in rt02 and is mandatory)
1 2 1 (include quetion 2 in rt01 and is mandatory)
1 1 1 (include quetion 1 in rt01 and is mandatory)
怀疑
我想根据数据库中的内容在前端显示选中或未选中的复选框。你知道如何根据这个 db 值检查前端的复选框吗?
使用该数据库信息,前端应如下所示:
表格:
<table class="table table-striped table-responsive-sm">
<thead>
<tr>
<th scope="col">Questions</th>
<th scope="col">Include for registration type</th>
<th scope="col">Mandatory</th>
</tr>
</thead>
<tbody>
@foreach($question as $q)
<tr>
<td>{{$q->question}}</td>
<td>
@foreach($registration_types as $rtype)
<div class="form-check">
<input autocomplete="off" name="include[{{ $q->id }}][{{ $rtype->id }}]" class="form-check-input {{$rtype->name}}" type="checkbox" value="1" id="{{$rtype->id}}">
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}}
</label>
</div>
@endforeach
</td>
<td>
@foreach($registration_types as $rtype)
<div class="form-check">
<input autocomplete="off" name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
class="form-check-input mandatorycheckbox" type="checkbox" value="1" id="{{$rtype->id}}">
<label class="form-check-label" for="exampleRadios1">
for the registration type "{{$rtype->name}}"
</label>
</div>
@endforeach
</td>
</tr>
@endforeach
</tbody>
</table>
问题模型
class Question extends Model
{
public function registration_type(){
return $this->belongsToMany('App\RegistrationType', 'registration_type_questions');
}
}
注册类型模型
class RegistrationType extends Model
{
public function conference(){
return $this->belongsTo('App\Conference');
}
public function questions(){
return $this->belongsToMany('App\Question', 'registration_type_questions');
}
}
会议模型
class Conference extends Model
{
// A conference has many registration types
public function registrationTypes(){
return $this->hasMany('App\RegistrationType', 'conference_id');
}
}
【问题讨论】: