'Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs

I'm currently learning laravel and creating my first form. Everything is awesome until I want to use {{ old('') }} helper in my blade file for radio buttons. I'm not sure how to go about doing it properly, and I can't seem to find much info on here about it either.

The code I have is as follows:

<div class="form-group">
    <label for="geckoHatchling">Gecko Hatchling?</label>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1">
            Yes
        </label>
    </div>
    <div class="radio">
        <label>
            <input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" checked>
            No
        </label>
    </div>
</div>


Solution 1:[1]

I think the following is a little bit cleaner:

<input type="radio" name="geckoHatchling" value="1" @if(old('geckoHatchling')) checked @endif>

<input type="radio" name="geckoHatchling" value="0" @if(!old('geckoHatchling')) checked @endif>

@if is checking the truthiness of the old value and outputting checked in either case.

Solution 2:[2]

You can use Form-helper, but it doesn't come out of the box with Laravel. You should install it manually. Please read the docs.

WITH FORM-HELPER

1. Blade

{!! Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio')) !!}
{!! Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio')) !!}

2. PHP

echo Form::radio('geckoHatchling', '1', (Input::old('geckoHatchling') == '1'), array('id'=>'geckoHatchlingYes', 'class'=>'radio'));
echo Form::radio('geckoHatchling', '0', (Input::old('geckoHatchling') == '0'), array('id'=>'geckoHatchlingNo', 'class'=>'radio'));

WITHOUT FORM-HELPER

1. Blade

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" @if(Input::old('geckoHatchling')) checked @endif>
<input type="radio" name="geckoHatchling" id="geckoHatchlingNo" value="0" @if(!Input::old('geckoHatchling')) checked @endif>

2. PHP

<input type="radio" name="geckoHatchling" value="1" class="radio" id="geckoHatchlingYes" <?php if(Input::old('geckoHatchling')== "1") { echo 'checked'; } ?> >
<input type="radio" name="geckoHatchling" value="0" class="radio" id="geckoHatchlingNo" <?php if(Input::old('geckoHatchling')== "0") { echo 'checked'; } ?> >

Solution 3:[3]

I have tried the accepted answer's solution, it doesn't work for me,

I had to use this {{}} curly braces (which is blade template's echo) in order to use the conditional statement.

<input type="radio" name="geckoHatchling" id="geckoHatchlingYes" value="1" {{(old('geckoHatchling') == '1') ? 'checked' : ''}}>

Solution 4:[4]

I have stumbled across what I believe will be to be a more accurate and comprehensive answer to this question. The problem with the main answer provided is the last radio button will always be selected moving forward. However, I have provided a code example below to show how to resolve this issue. (PS - this works in Laravel 8, and, I'm only assuming it will work for previous versions)

<div> <input type="radio" name="role" value="teacher" @if(old('role') == 'teacher') checked @endif> </div>
        
<div> <input type="radio" name="role" value="student" @if(old('role') == 'student') checked @endif> </div>
        
<div> <input type="radio" name="role" value="assistant" @if(old('role') == 'assistant') checked @endif> </div>

Just using:

@if(old('role') checked @endif

will always result in your last radio button being selected if you have multiple radio buttons.

Solution 5:[5]

befor Laravel 9

<input type"radio" name="active" value="1" {{old('active',user->active))?'checked',''}}/>
<input type"radio" name="active" value="0" {{old('active',user->active))?'','checked'}}/>

Laravel 9

<input type"radio" name="active" value="1" @checked(old('active',$user->active))/>
<input type"radio" name="active" value="0" @checked(!old('active',$user->active))/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Dharman
Solution 5 Jahongir Tursunboyev