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

eLekun's avatar

Problem updating record with checkbox

I am trying to update student attendance records, but the values ​​are getting incorrectly to the controller.

Form where I send the data to update the attendance record, that is, select checkbox to mark the student as present, or leave the checkbox blank to insert it as absent in the database in the attendance table

Checkboxs:

      <input type="checkbox"  name="asistencia[]" value="{{$alumno->estado}}"<?php echo ($alumno->estado == 1 ? ' checked' : '')?> class="form-check-input settings" id="checkAsistencia">
      <input type="hidden" name="asistenciaId[]" value="{{$alumno->asistenciaId}}">

Controller:

public function enviarAsistencia(Request $request, $idCurso){


    //estado de la asistencia , es el checkbox 1 presente 2 ausente
    $estadoAsistencia = $request->asistencia;

    //id de la asistencia del alumno
    $asistenciaId = $request->asistenciaId;

    foreach($asistenciaId as $index => $id) {
    
        
        $estado = (isset($estadoAsistencia[$index])) ? 1 : 2;  
        $asistencia = Asistencia::find($id); 
        $asistencia->estado = $estado;
        $asistencia->save();
        
    }
return back()->with('Listo','Asistencia ingresada correctamente');
}
0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

I would use the hidden input method here, assuming you have a random and unpredictable number of id's

The hidden field is used when the checkbox is not checked ensuring that you always have a value returned.

<input type="hidden" name="asistencia[{{$alumno->asistenciaId}}]" value="0" />
<input type="checkbox"  name="asistencia[{{$alumno->asistenciaId}}]" value="1" 
    {{ $alumno->estado == 1 ? ' checked' : '') }} 
    class="form-check-input settings" />

what you end up with is an array of asistencia in which the key is the student and the value will be 0 or 1

1 like
eLekun's avatar

Hi! Really friend, thank you very much I had been with this problem for a while, thank you very much. excuse my English thank you!!!

Snapey's avatar

you are welcome. Your english is fine.

1 like

Please or to participate in this conversation.