Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bekaskaki's avatar

show if checkbox is checked or unchecked in edit view

how to show checked unchecked in multiple checkbox laravel edit blade view

ps: array data

edit blade view :

<form method="POST" action="{{url('user/rencana-kerjasama/edit')}}" enctype="multipart/form-data" class="form-horizontal">
{{ csrf_field() }} 
 <div class='col-md-6'>
<div class="checkbox">
  <label>
  <input type="checkbox" name="bidang[]"  value="Pendidikan" @if(is_array(old('bidang')) && in_array('Pendidikan', old('bidang'))) checked @endif>Pendidikan
 </label>
  </div>
<div class="checkbox">
 <label>
<input type="checkbox" name="bidang[]" value="Penelitian" > Penelitian
 </label>
</div>
 <div class="checkbox">
 <label>
  <input type="checkbox" name="bidang[]"  value="Pengabdian Masyarakat"> Pengabdian Masyarakat
 </label>
 </div>
</div>
</form>

controller :

 public function edit($id)
    {
        $title = $this->title;
        $data = RencanaKerjasama::find($id);
        return view('user.' . $title . '.edit', compact('title', 'data'));
    }

i use this but not work

@if(is_array(old('bidang')) && in_array('Pendidikan', old('bidang'))) checked @endif

0 likes
10 replies
Sergiu17's avatar
<input
    type="checkbox"
    name="bidang[]"
    value="Pendidikan"
    {{ (is_array(old('bidang')) && in_array('Pendidikan', old('bidang'))) ? ' checked' : '' }}
/>
bekaskaki's avatar

i try this but not work :

<div class="checkbox">
  <label>
  <input type="checkbox" name="bidang[]"  value="Pendidikan" {{ (is_array(old('bidang')) && in_array('Pendidikan', old('bidang'))) ? ' checked' : '' }}>Pendidikan
 </label>
  </div>

is there anything I have to change in the controller?

jlrdw's avatar
jlrdw
Best Answer
Level 75

Or you trying to get old value if edit fails or display the value from database when edit page loads.

If the second case, you have to actually get the data yourself from the database, example for a single checkbox:

<input type="checkbox" name="adopted" id="adopted" value="1" {{  ($cat->adopted == 1 ? ' checked' : '') }}

In other words do an if like I showed to see if the value was a 0 or a 1. In your case you have to loop since it's an array.

I usually like individual checkboxes to avoid having to loop over them.

https://laracasts.com/discuss/channels/general-discussion/blade-checkbox-and-eloquent

1 like
bekaskaki's avatar

for the second case

i use multiple checkbox because I have a unit field in the table. The field will contain several values such as unit1, unit2, unit3, unit4. so I use the array data type to enter the data into one field.

I have another question, which one is better to use the array data type or create a field in the table for each unit?

jlrdw's avatar

The array is fine, but you have to loop over it.

viper75x's avatar

@jlrdw I've got a similar problem with an edit form.

stocked == 1 ? 'checked' : '0') }}>Stocked

The stocked field is boolean. And if I set an input textbox to display the stored stocked value, I can see the value. But no matter what I do with the example you provided, nothing is setting the checkbox to 'checked'.

What could I be missing?

(I'm not familiar with the ? operator yet, so I'm going to find out how that works, but I think I've got the gist based on the usage.)

Vixo's avatar

You should create your own thread, this one is 1 year old. Also please put your codes between three single backquotes ```

ImWaller's avatar

The ternary ?: operator is like if else. Read more here

Your code, if stocked is equal with 1 mean true then return check else 0 is mean false.

viper75x's avatar

<input name="stocked" type="checkbox" class="form-check-input" value="" {{ ($item->stocked == 1 ? 'checked' : '')}}>

Ok, thanks.

The above code seems to work. I've looked at 3 different records so far and this one shows whether or not it is stocked.

Please or to participate in this conversation.