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.
Oct 26, 2020
3
Level 1
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;
}
Please or to participate in this conversation.