Reputation: 915
I have the following query that brings selected values from the DB (i.e. preferences that were entered by user through around 20 checkboxes):
$brng_selected = DB::table('Cprefs')
->where('user_id', $u_id)
->get();
return view('prefCat', ['b_sel' => $brng_selected]);
In the following example the user selected a preference with an id of 11 which refers to "sports" (in qatype field):
array(1) {
[
0
]=> object(stdClass)#368 (6) {
[
"id"
]=> int(47) [
"created_at"
]=> string(19) "2019-09-27 12:29:59" [
"updated_at"
]=> string(19) "2019-09-27 12:29:59" [
"user_id"
]=> int(12) [
"qatype"
]=> int(11) [
"type"
]=> int(1)
}
}
Now I need to configure the sports checkbox below to be checked based on the above. How can this be done?
<input type="checkbox" name="catpref[]" value=11 > Sports<br>
<input type="checkbox" name="catpref[]" value=1> Politics<br>
Upvotes: 0
Views: 698
Reputation: 5731
Get the column which you need to compare as below
$brng_selected = DB::table('Cprefs')
->select('qatype') // column name which you need
->where('user_id', $u_id)
->get()->toArray(); // it will return as array
return view('prefCat', ['b_sel' => $brng_selected]);
In your view change as below :
<input type="checkbox" name="catpref[]" value=11 {{ (in_array(11,$brng_selected)?"checked":'') }} > Sports<br>
Upvotes: 1
Reputation: 19
Something like this?
@if ($ba_sel->qa_type == 11)
<input type="checkbox" name="catpref[]" value="11" checked>Sports<br>
@else
<input type="checkbox" name="catpref[]" value="11">Sports<br>
@endif
Upvotes: 1