Jimmyjbk
Jimmyjbk

Reputation: 411

How to use old values or values from the database in a form with checkbox laravel

I would like to use the value from the database so that if a vaccine is checked, it stays that way when I am editing value from my database. I have used the same concept with the select button but do not know how to do the same with checkbox.

for select button, I used this

<div class="col-md-6">
        <select name="nutrition" class="form-control">
            <option value="" disabled>Nutrition</option>
            <option value="Healthy"  {{ $clinic->nutrition == 'Healthy' ? 'selected' : '' }}>Healthy</option>
            <option value="Not Healthy" {{ $clinic->nutrition == 'Not Healthy' ? 'selected' : '' }}>Not Healthy</option>
        </select>
    </div>

What can I use for this

  <div class="form-group row pl-5">
            <label for="vaccine1" class="col-md-4 col-form-label text-md-right">Brucellosis</label>
            <div class="col-md-6">
                <input class="form-control" type="checkbox" id="checkbox" name="vaccine1" value="Brucellosis">
            </div>
        </div>
        <div class="form-group row pl-5">
            <label for="vaccine3" class="col-md-4 col-form-label text-md-right">East Cost Fever</label>
            <div class="col-md-6">
                <input class="form-control" type="checkbox" id="checkbox" name="vaccine3" value="East Cost Fever">
            </div>
        </div>

Upvotes: 0

Views: 35

Answers (1)

pr1nc3
pr1nc3

Reputation: 8348

    <input class="form-control" type="checkbox" id="checkbox" name="vaccine3" value="East Cost Fever"  {{ $myValues->value== 'East Cost Fever' ? 'checked' : '' }}>

You can apply the same logic by checking the variable you want to match the value of the checkbox, or the name of the checkbox would be more preferable.

If your statement is true just set the checkbox to checked exactly like you did for the dropdown.

Upvotes: 1

Related Questions