I am trying to create an array of objects in laravel php. I have achieved this so far.
but it end up creating a singlee object instead of an array…
I get this output
$usersData = User::where('user_id', 2)->where("stud_class", $exam_details->exam_class_id)->where("XXXX", $exam_details->exam_branch_id)->get(['name', 'id']);
foreach ($usersData as $user) {
$studentsArray = array(
"student" => $user->name,
"subjects" => []
);
foreach ($exam_data as $subject) {
$att_exams = MODEL::where('XXXXX', $subject->subject_id)
->where('XXXX', $user->id)
->first();
if ($att_exams) {
$marks = MODEL::where('XXXX', $att_exams->attended_exams_id)->get();
$right = 0;
$wrong = 0;
$total_marks = $marks->sum('XXXX');
foreach ($marks as $mark) {
if ($mark->XXX== 0) {
$wrong++;
} else {
$right++;
}
}
$total_negative_marks = $wrong * $subject->negative_marks;
$subjectsArray[] = array(
"subject" => $subject->subject_name,
"marks_" => $total_marks - $total_negative_marks,
);
} else {
$subjectsArray[] = array(
"subject" => $subject->subject_name,
"marks_" => 0,
);
}
}
$studentsArray["subjects"] = array($subjectsArray);
}
return $studentsArray;
