我正在尝试在laravel php中创建一个对象数组。到目前为止我已经实现了这一点。
当我返回数组时,我得到的响应是,为了清晰起见,我添加了一个图像。
我得到这个输出
我想创建这样的响应...
[
{
"student": "Jaime Thomas",
"subjects": [
{
"subject": "Physics",
"marks_": 0
},
{
"subject": "Chemistry",
"marks_": 0
}
]
},
{
"student": "Jaime Dey",
"subjects": [
{
"subject": "Physics",
"marks_": 0
},
{
"subject": "Chemistry",
"marks_": 0
}
]
}
]
$usersData = User::where('user_id', 2)->where("stud_class", $exam_details->exam_class_id)->where("XXXX", $exam_details->exam_branch_id)->get();
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');
$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;
1条答案
按热度按时间jgzswidk1#
你可以试试下面的代码: