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

carincon93's avatar

input checkboxes (checked) from array associative

I need to checked multiple input checkbox from an array

this $rol->permisos show this array:

array:2 [▼
 "editar-usuario" => true
 "editar-rol" => true
]
@foreach ($rol->permisos as $key => $permiso)
                           <input type="checkbox" name="" value="" {{ $key == 'editar-rol' ? 'checked' : ''}}>1
                           <input type="checkbox" name="" value="" {{ $key == 'crear-rol' ? 'checked' : ''}}>2
@endforeach

but this code show 6 input (it multiplies the quantity items in the array)

0 likes
2 replies
Cinek's avatar
Cinek
Best Answer
Level 6

You don't need a foreach:

<input type="checkbox" name="" value="" {{ !empty($rol->permisos['editar-rol'] ? 'checked' : ''}}>1
<input type="checkbox" name="" value="" {{ !empty($rol->permisos['editar-rol'] ? 'checked' : ''}}>2

Why you have checkbox with editar-rol twice?

Please or to participate in this conversation.