2

How to make a radio button checked in laravel collectives?

{{Form::radio('gender','1',['class'=>'form-check-input'])}}

I'm taking the value from the database and I need to select the appropriate gender value.

$data->gender

Full code is as follows

<div class="col-sm-9" style="margin:auto">
    <div class="form-check form-check-inline">
        {{Form::radio('gender','1',['class'=>'form-check-input'])}}
        <label class="form-check-label ml-2" for="inlineRadio1">Male</label>
    </div>
    <div class="form-check form-check-inline">
        {{Form::radio('gender','2',['class'=>'form-check-input'])}}
        <label class="form-check-label ml-2" for="inlineRadio2">Female</label>
    </div>
</div>
harinsamaranayake
  • 751
  • 2
  • 12
  • 32

2 Answers2

3

The data to populate your form should go on your Form::model() method.

Something like this:

On the controller function

// on the controller function
$data['gender'] = 1;
return view('my-view')
    ->with('data' $data);

On the template

{{ Form::model($data/*, ...*/) }}
<div class="col-sm-9" style="margin:auto">
    <div class="form-check form-check-inline">
        {{Form::radio('gender','1',['class'=>'form-check-input'])}}
        <label class="form-check-label ml-2" for="inlineRadio1">Male</label>
    </div>
    <div class="form-check form-check-inline">
        {{Form::radio('gender','2',['class'=>'form-check-input'])}}
        <label class="form-check-label ml-2" for="inlineRadio2">Female</label>
    </div>
</div>
{{ Form::close() }}
Gus Costa
  • 609
  • 5
  • 13
2

Add true before options array and empty '' on others

{{ Form::radio('gender', 1, true, ['class'=>'form-check-input', 'id' => 'inlineRadio1']) }}
{{ Form::label('inlineRadio1', 'Male', ['class' => 'form-check-label']) }}

{{ Form::radio('gender', 2, '', ['class'=>'form-check-input', 'id' => 'inlineRadio2']) }}
{{ Form::label('inlineRadio2', 'Female', ['class' => 'form-check-label']) }}
Boss COTIGA
  • 893
  • 7
  • 15