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

dmhall0's avatar

How to Link Checkbox Arrays

I have a table with 2 columns of checkboxes; 1 for Select and 1 for Required.

<thead>
  <tr>
     <th>Select</th>
     <th>Required?</th>
     <th data-sortable="true">Skill</th>
  </tr>
</thead>
<tbody>
  @foreach($skills as $skill)
    <tr>
      <td><input type="checkbox" name="skill_id[]" value="{{ $skill->id }}"></td>
      <td><input type="checkbox" name="required[]" value="1"></td>
      <td>{{ $skill->skill_name }}</td>
    </tr>
  @endforeach
</tbody>

When I dd($request) in my controller...

"skill_id" => array:2 [▼
        0 => "2"
        1 => "5"
      ]
      "required" => array:1 [▼
        0 => "1"
      ]

For example I selected skill_id #2 and #5 but only required #5. As you can see the checkbox arrays are not connected / linked letting me know which was required. How can I link them in my request?

Thanks for the help!

0 likes
6 replies
MichalOravec's avatar

Use only one checkbox for that.

<input type="checkbox" name="required[{{ $skill->id }}]" value="1">
dmhall0's avatar

I think I need both checkboxes. The "select" checkbox links them to a user, with the "required" checkbox says whether it is required (checked) or optional (not checked).

jlrdw's avatar

I don't understand the required checkbox. Just use a label Required and validate that one is checked.

And use the request has method, or loop array to verify one was checked.

NOTE: A checkbox not checked nothing is passed, you yourself have to verify at least one was checked.

Sorry I don't understand well your checkboxes.

dmhall0's avatar

I have a list of skills and users. Linking a skill to a user the skill can either be optional or required. In my skill_user table I have skill_id and required fields. Required field is boolean. So when I check the "selected" box I need to pull in the associated skill_id; but when I also check the "required" box I need to update to required field to "1". I hope that helps. Sorry for confusion.

jlrdw's avatar

In your example:

For example I selected skill_id #2 and #5 but only required #5.

Is 5 always required and 2 optional.

If that is the case, #5 could be done in some custom validation. And have the checkbox for required already checked.

Sort of like:

if required5 is true then skill5 needs to be true.

You may want to have unique id's here and individual checkboxes so you have a one to one match.

Otherwise if storing as json, you can't leave any holes.

example

1,0,0,1,1

You have to have the 0 position filled if using an array, and they have to match:

required    1,0,0,1,1
skills----- 1,0,0,1,1

//  so required position 4 is 1, thus skill position 4 has to match.

//  required position 2 is 0, thus skill position 2 does not have to match.

But skill can be:
skills----- 1,1,1,1,1

But I don't know how you are making it required if not pre-checked showing it's required.

As long as required 1 matches, I guess others are optional.

dmhall0's avatar
dmhall0
OP
Best Answer
Level 1

I have found a way to make it work... one checkbox is for "required" and the other checkbox is for "optional". I then loop through each array in my controller insert the record and updating the required field accordingly. Not ideal, but it will work. Thanks for the help.

Please or to participate in this conversation.