I have a form that has some questions/fields (email, phone number,...) and then the user can select if that field should be included for the registration types that exist for a conference and also if that field should be mandatory or not for each registration type, through checkboxes.
Based on the selected checkboxes the info is inserted in the pivot table "registration_type_questions" that has this structure: registration_type_id, question_id, required.
The database is like this for now:
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)
Doubt
I want to show in the frontend the checkboxes checked or not based on what is in the db. Do you know how is possible to check the checkboxes on the frontend based on this db values?
With that db info the frontend should appear like below:
Form:
<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>
Question Model
class Question extends Model
{
public function registration_type(){
return $this->belongsToMany('App\RegistrationType', 'registration_type_questions');
}
}
Registration type model
class RegistrationType extends Model
{
public function conference(){
return $this->belongsTo('App\Conference');
}
public function questions(){
return $this->belongsToMany('App\Question', 'registration_type_questions');
}
}
Conferece model
class Conference extends Model
{
// A conference has many registration types
public function registrationTypes(){
return $this->hasMany('App\RegistrationType', 'conference_id');
}
}
答案 0 :(得分:1)
在控制器中将关系值存储到变量并将其传递给视图。
$optional
将required
和$question
传递给视图。使用@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}}" {{ !empty($optional[$q->id][$rtype->id]) ? "checked" : "" }}>
<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}}" {{ !empty($required[$q->id][$rtype->id]) ? "checked" : "" }}>
<label class="form-check-label" for="exampleRadios1">for the registration type "{{$rtype->name}}"</label>
</div>
@endforeach
</td>
</tr>
@endforeach
。
在您的视图中,检查当前复选框是否在变量中定义了关系,然后复选框属性&#34; 选中&#34;。
{{1}}
答案 1 :(得分:0)
根据数据库值添加或删除checked
属性。
@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"
id="{{$rtype->id}}"
{{ array_search($rtype->id, $q->registration_type->pluck('id')->all()) !== FALSE
? 'checked' : '' }}>
<label class="form-check-label" for="exampleRadios1">
{{$rtype->name}}
</label>
</div>
@endforeach
为了提高性能,您可以从控制器准备$q->registration_type->pluck('id')->toArray()
,因此您不必为每种注册类型运行这些功能。
答案 2 :(得分:0)
在您的表单中,您需要添加:
<input autocomplete="off"
name="mandatory[{{ $q->id }}][{{ $rtype->id }}]"
class="form-check-input mandatorycheckbox"
type="checkbox"
value="1"
id="{{$rtype->id}}"
{{$rtype->required==1?" checked":""}}>
您发现未勾选的广播/复选框不会随表单一起提交。这意味着您需要先检查它们的存在(isset)。
我注意到的另一个问题。在您提供的数据示例中,当您更新{1}}时,它将更新哪条记录(此代码的代码可能在其他位置未提供)。