所以我有一个
学生模特, 主题模型, MARKS模型,其中包含总分字段
我想要做的是获得班上每个学生的考试总分,然后根据班级中最高分检索结果,然后将这些分数定位为1、2、3……
然后最终得到一个学生的职位,并将其存储在考试记录表中.
到目前为止,我能够实现获得每个学生的考试总分,将它们存储在一个数组中,根据最高分进行排序,
现在唯一让我头疼的问题是从数组中获取这些分数的位置,我的问题是如何获得学生的分数位置,或者是否有方法将位置添加到分数中,然后检索学生的位置
例如
1 student_id => 2, total_score => 500
2 student_id => 3, total_score => 455
3 student_id => 5, total_score => 345
这是我的代码下面,请任何人有 idea 如何解决这个问题,我需要你的帮助.
TextInput::make('position')->numeric(
function (Closure $set) {
// Get all students from class
$studentsInClass = $this->class->students;
//empty array to store student's total_score
$totalScore = [];
// loop through students get all their total_score on all subjects from mark table and sum it, then store it
in totalScore array.
foreach ($studentsInClass as $key => $student) {
$totalScore[] = array('student_id' => $student->id, 'total_score' => $student->marks->sum('total_score') );
}
// Sort scores from highest to lowest
$sortedScores= array_values(array_reverse(Arr::sort($totalScore, function ($value) {
return $value['total_score'];
})));
// get the current student Id
$id = $this->student->id;
// find a student in the array that matches the current student id and return his score.
//so this is where I would like to return the score and position of the student
$filteredArray = Arr::where($sortedScores, function ($value, $key) use ($id) {
return $value['student_id'] == $id;
});
}
)->disabled(),
如果您dd($sortedScores)