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

shing_shing's avatar

Foreach is not working

Good day, everyone!

This is my problem: The first student's today's attendance should be late, it always displays present. And as I return the $attendsubj, it's a different $student->id. That $student->id is looping the $students collection, so i get the present status of all students.

    	$classsubjectsunique = collect($classsubjects)->unique('id')->values()->all();

            foreach($students as $student)
            {
                
                $subjectsattendance = array();

                if(count($classsubjectsunique)>0)
                {
                    foreach($classsubjectsunique as &$classsubject)
                    {
                        $attendsubj = DB::table('studentsubjectattendance')
                            ->where('student_id', $student->id) // here is the id looping each of the students
                            ->where('subject_id', $classsubject->id)
                            ->where('section_id', $request->get('sectionid'))
                            ->where('date', $date)
                            ->where('deleted', '0')
                            ->first();
                            
                        if($attendsubj)
                        {
                            $classsubject->status = $attendsubj->status;
                            
                        }else{

                            $classsubject->status = "";

                        }
                        array_push($subjectsattendance, $classsubject);
                    }
                }
                
                $student->subjectattendance = $subjectsattendance;
                
                $advisoryattendance = DB::table('studattendance')
                    ->where('studid', $student->id)
                    ->where('syid', $syid)
                    ->where('deleted', 0)
                    ->where('tdate', $date)
                    ->first();

                if($advisoryattendance)
                {
                    //1 = present; 2 = late or tardy; 3 = halfday or cc ; 4 = absent
                    if($advisoryattendance->present == 1)
                    {
                        $attstatus = '1';
                    }
                    if($advisoryattendance->absent == 1)
                    {
                        $attstatus = '4';
                    }
                    if($advisoryattendance->tardy == 1)
                    {
                        $attstatus = '2';
                    }
                    if($advisoryattendance->cc == 1)
                    {
                        $attstatus = '3';
                    }

                }else{
                    $attstatus = null;
                }
                
                $student->classattendance = $attstatus;
                
        
            }
0 likes
3 replies
bdavila's avatar

If $advisoryattendance has multiple rows with the same studid you might not be getting the most recent record. Try adding ->orderBy('id', 'desc') before ->first(). Where id is your primary key.

shing_shing's avatar

I still get the same. I think the problem is not in the advisory attendance but in the subjectattendance.

I have the first student id which is 1 and the last student which is 10. As i run the loop the $student->id returning the $attendsubj is the last student with the id, 10.

shing_shing's avatar

I fixed it.

I change my code from this:

foreach($classsubjectsunique as $classsubject)
                {
                    $attendsubj = DB::table('studentsubjectattendance')
                        ->where('student_id', $studentid)
                        ->where('subject_id', $classsubject->id)
                        ->where('section_id', $request->get('sectionid'))
                        ->where('date', $date)
                        ->where('deleted', '0')
                        ->orderByDesc('id')
                        ->first();
                        
                    if($attendsubj)
                    {
                        $classsubject->status = $attendsubj->status;
                        
                    }else{

                        $classsubject->status = "";

                    }
                    array_push($subjectsattendance, $classsubject); // here
                }

to this so i can still have the subject's details:

                    foreach($classsubjectsunique as $classsubject)
                    {
                        $attendsubj = DB::table('studentsubjectattendance')
                            ->where('student_id', $student->id)
                            ->where('subject_id', $classsubject->id)
                            ->where('section_id', $request->get('sectionid'))
                            ->where('date', $date)
                            ->where('deleted', '0')
                            ->orderByDesc('id')
                            ->first();
                            
                        if($attendsubj)
                        {
                            $classsubject->status = $attendsubj->status;
                            
                        }else{

                            $classsubject->status = "";

                        }
                        array_push($subjectsattendance, (object)array(
                            'id'            => $classsubject->id,
                            'subjectname'   => $classsubject->subjectname,
                            'subjectcode'   => $classsubject->subjectcode,
                            'status'        => $classsubject->status
                        ));

}

but still, I want to know what's the problem tho.

Please or to participate in this conversation.