25

I am using Laravel 5.3,this is a form demo below:

<div class="container">
    <form>
        <div class="form-group row">
            <label for="name" class="col-sm-2 col-form-label">Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
            </div>
        </div>

        <fieldset class="form-group row">
            <legend class="col-form-legend col-sm-2">Gender</legend>
            <div class="col-sm-10">
                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
                        Male
                    </label>
                </div>
                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="gender" value="2" @if(old('gender',$student->gender)=="2") checked @endif>
                        Female
                    </label>
                </div>
            </div>
        </fieldset>
        <div class="form-group row">
            <label class="col-sm-2">Hobbies</label>
            <div class="col-sm-10">
                <div class="form-check">
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(...) checked @endif> football
                    </label>
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(...) checked @endif> basketball
                    </label>
                    <label class="form-check-inline">
                        <input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif> swimming
                    </label>
                </div>

            </div>
        </div>
        <div class="form-group row">
            <div class="offset-sm-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </form>
</div>

My question is:

I can show old data in input type="text" like this:

<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">

And I can show old data in input type="radio" like this:

<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>

But I don't know how to show old data in input type="checkbox":

<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif>

How to do it?

3

10 Answers 10

86

Update for Laravel 9.x as Yusuf T suggested (refer this document) :

<input type="checkbox" name="active" value="active" @checked(old('active', $someVariableOrValue )) />

For older versions this will work:

<div class="form-check">
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1, old('hobby'))) checked @endif> football
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(is_array(old('hobby')) && in_array(2, old('hobby'))) checked @endif> basketball
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(is_array(old('hobby')) && in_array(3, old('hobby'))) checked @endif> swimming
    </label>
</div>

Also as brokekidweb suggested :

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>

OR

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) && in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
6
  • 3
    Need to check is_array() couse if hobby[] will be empty( unchecked all checkboxes ) php will get error "<< in_array() expects parameter 2 to be array, null given >>
    – G.F.
    May 27, 2017 at 16:09
  • Updated the answer. Thanks @G.F. May 29, 2017 at 4:25
  • 4
    You can clean this up with ternary operators {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }} Blade also lets you use and instead of &&. Your answer does work as written. Oct 4, 2017 at 2:19
  • 1
    My colleague and I have been suffering figuring this out for like one hour. Many thanks. Aug 9, 2018 at 19:52
  • 1
    Nice, in the Laravel 9 version, you can also use @checked directive, e.g. <input @checked($conditionThatReturnsABoolean) />
    – Yusuf T.
    Jul 11, 2022 at 9:57
6

You can use

<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>

Or you could just switch to use Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons

And have code like this instead:

{!! Form::checkbox('name', 'value', true) !!}
5

1st WAY

First create variable

$hob = old('hobby');

and than check is array or not

echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL;

and then apply it.

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" <?php echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL; ?>> football

2nd WAY

OR you can do that but in this case you will not create variable

@if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif

and then apply it.

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif > football

1
  • This works better than the accepted answer for me, because, if no checkbox is checked no array is created, so you get the error: in_array() expects parameter 2 to be array, null given
    – Adocad
    May 1, 2017 at 12:51
4

These all appear to work, a cleaner solution that I adopt takes the form of the following...

<input type="checkbox" name="departments[]" value="{{ $item->id }}" {{ in_array($item->id, old('departments', [])) ? 'checked' : '' }}>

Where $item is the object while iterating over a collection,

Rather than explicitly checking if we have an array, we can defer the default value of the old value as being an array in the instance the form has yet to be submitted or there were no options checked on the original form request.

Referring back to OPs original query, it could be structured similar to the below

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ in_array(1, old('hobby', [])) ? 'checked' : '' }}>
2

My solution:

<input type="checkbox" class="custom-control-input" name="hobby" value="1"
   @if((old('_token') && old('hobby') != null) || (old('_token') == null && $employee->hobby))
   checked 
   @endif
>
1

Had to iterate through the $userHobbies array to pull out just the ID for each item then in the view changed the Form::checkbox to:

{{ Form::checkbox('hobby[]', $user->hobby, in_array($user->hobby, $hobbiesAvailableIds)) }}

Ref: http://laravel.io/forum/09-15-2014-how-to-show-checkboxes-as-checked-when-values-are-set-in-the-database

1

Use flash data (Laravel Flash Data) to set the information for Blade about checked/unchecked checkbox.

My example: checkbox "legal" which should be accepted.

In Controller:

  1. Put checkbox in validator

    'legal'  => 'required|accepted'
    
  2. After validator set flash data:

    $request->session()->flash('legal', 'true');
    

In Blade:

<input class="form-check-input" type="checkbox" name="legal"
id="legal" {{Session::has('legal') ? 'checked' :''}}
0

Try this:

$hobby = explode(', ', $data->hobby);

<input class="form-control" name="hobby[]" type="checkbox" value="cricket" <?=(in_array('cricket',$hobby) ? 'checked="checked"' : '')?>> 
0

This snippet uses an array of Timesheet models and I want to be able to select a set of these using checkboxes. The tickbox should be initially ticked if the value of sales_invoice_id is not null. Note that I set indexes on my array because this makes it much easier to analyse the POST data later.

@foreach ($timesheets as $ts)
  <input type="checkbox" name="ts_id[{{$ts->id}}]" value="1"
      @if (array_key_exists($ts->id, old('ts_id', ($ts->sales_invoice_id != null) ? [$ts->id => 1] : [] )))
         checked
      @endif
  >
@endforeach

This works by pulling the "old" array of timesheet checkboxes and looking to see if the timesheet we are looking at is in the list using array_key_exists() However the first time the form is displayed there is no "old" session data so the 2nd parameter to old() provides the default value. The default value is set to an array of 1 item if the checkbox should be ticked, or an empty array if not.

-1

@

foreach($brands as $key => $brand)
                             <label class="container col-sm-3">{!! $brand->name !!}
                                    <input type="checkbox" class="brand_id" id="brand_id" name="brand_id[]"
                                     value="{{@(!empty($brand->id) ? $brand->id : "")}}"
                                    @foreach($brand_sel as $key => $_sel)
                                     {{($brand->id == $_sel->brand_id ? "checked='checked'": '')}}
                                    @endforeach()
                                     />
                                    <span class="checkmark"></span>
                                </label>
                            @endforeach()
1
  • 1
    Why this should answer OP question ?
    – executable
    Feb 1, 2019 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.