i want to make checkbox with 2 values – One with “Yes” and other with “No” and send to database. I try to make it when is not send “Yes” to give me “No” but now dont work. I would be grateful if you could tell me how to do it and what are the errors in my code
<form method="post" action="/adminpanel/hofswitch">
@csrf
<div class="card-body">
@foreach($char as $value)
<div class="card-body">
@if($value->status == "Yes")
<input type="hidden" name="id[]" value="{{$value->id}}">
<input type="checkbox" name="switch[]" value="Yes" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputSuccess"><i class="fas fa-check"></i> Character Class</label>
<input type="text" name="class" value="{{$value->class}}" class="form-control is-valid" id="inputSuccess" readonly="true" placeholder="{{$value->class}}">
</div>
@else
<input type="hidden" name="id[]" value="{{$value->id}}">
<input type="checkbox" name="switch[]" value="Yes" data-bootstrap-switch data-off-color="danger" data-on-color="success">
<div class="form-group">
<label class="col-form-label" for="inputError"><i class="far fa-times-circle"></i> Character Class</label>
<input type="text" name="class" value="{{$value->class}}" class="form-control is-invalid" id="inputError" readonly="true" placeholder="{{$value->class}}">
</div>
@endif
</div>
@endforeach
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary col-12">Submit</button>
</div>
</form>
And controller
public function hof_switch(Request $request)
{
foreach ($request->id as $i => $id) {
$switch = $request->switch;
if (!isset($request->switch))
{
$switch = "No";
}
$update = DB::connection('XXX')->table('XXX_HOF')
->where('class', $request->class[$i])
->update(
[
'status' => $switch[$i],
]);
}
return redirect()->back()->withSuccess('You have switch this class successfully!');
}