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

sharoonamjid's avatar

How to append user id with checkbox value?

I'm trying to store attendance of college students on single submit button, a teacher would mark 'present' or 'absent' checkbox, once all checkboxes are selected I want to store the values of these checkboxes as well as the id's of the student in the database. List of students is coming from the database using foreach() function.

So how can I append the student id# with checkbox value so that in the controller I get both, the user Id as well as the 'present' or 'absent' value?

Thank you,

0 likes
4 replies
shakti's avatar

in the foreach you can name them dynamically i.e. name of every checkbox include student-id you should call that foreach in the controller as well

@foreach($student as $stud){
<input type="hidden" name="{{$stud->id}}" value="0">
<input type="checkbox" name="$stud->id" value="1">present
@endforeach 
3 likes
sharoonamjid's avatar

@ershakti thank you, I see some hope in your answer, can you please give a code example of how can I get student id and 'absent', 'present' status in the controller and inserting it in Attendance model.

Thank you very much

shakti's avatar
foreach($student as $stud)
{
    $studid=$stud->id;
     $evento = $request->student()->attend()->create([
      'stdid' => $studid,
      'status'=>($request->$studid ==1)?'p':'a',
      'data'=>time()
      ]);
}

Please checko this might help
patrykszady's avatar

I would do something like this:

In your view:

@foreach ($students as $student)

    <ul class="list-group">
        <li class="list-group-item"><input type="checkbox" checked value="1" name="stud_id[]">Present</li>
<li class="list-group-item"><input type="checkbox" value="2" name="stud_id[]">Absent</li>
    </ul>

//Put Hidden Input here with the $student->id
@endforeach

In your controller:

    $count = count($request->student_id);
    for($i = 0; $i < $count; ++$i){
        if($request->student_id[$i] == null){
        //If this student doen't have 1 or 2 checked, do nothing. Not really for your case scenario but I don't see why not leave this as a safeguard. 
        } else {
            //CREATE this student attendance row.
        }       
    }   

Please or to participate in this conversation.