[英]Displaying total answer and correct answer of each user
I have a db structure like this: 我有这样的数据库结构:
Tables: 表:
users(id, email, password, ...) //default laravel users table
users(id,email,password,...)//默认的laravel users表
examinees(id, user_id, ...)
被考生(id,user_id,...)
exam_quizzes(id, title, explanation)
Exam_quizzes(ID,标题,说明)
exam_quiz_answers(id, title, exam_quiz_id, is_correct_ans)
exam_quiz_answers(ID,标题,exam_quiz_id,is_correct_ans)
submitted_answers(id, user_id, exam_quiz_id, exam_quiz_answer_id)
已提交的答案(id,user_id,exam_quiz_id,exam_quiz_answer_id)
I already have the respective models and relationship methods set up. 我已经设置了相应的模型和关系方法。
Models: 楷模:
User, Examinee, ExamQuiz, ExamQuizAnswer, SubmittedAnswer
Relationships: 关系:
// User -> hasOne() -> Examinee
$user->examinee
// ExamQuiz -> hasMany() -> ExamQuizAnswer
$examQuiz->examQuizAnswers
// SubmittedAnswer -> hasMany() -> ExamQuiz
$submittedAnswer->examQuizzes
// SubmittedAnswer -> hasMany() -> ExamQuizAnswer
$submittedAnswer->examQuizAnswers
// User -> hasMany() -> SubmittedAnswer
$user->submittedAnswers
In my view, how can I display the Name , Total Answered and Total Correct for every user who is also an examinee, in a table like this: 在我看来,如何在每个表中为也是考生的每个用户显示Name , Total Answered和Total Correct :
<tr>
<th>Name</th>
<th>Answered</th>
<th>Correct</th>
</tr>
@foreach()
{{-- I have no idea what to do here --}}
<tr>
<td></td>
<td></td>
<td></td>
</tr>
@endforeach
In your controller, u get the users an pass it to view. 在您的控制器中,您可以让用户通过查看。
$users = User:get();
and the make a foreach loop to get the answer and correct answers: 并进行foreach循环以获取答案和正确答案:
<tr>
<th>Name</th>
<th>Answered</th>
<th>Correct</th>
</tr>
@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{count($user->submittedAnswers()->get())}}</td>
@php
foreach($user->submittedAnswers()->get() as $answer){
foreach($answer->examQuizAnswers->get() as $quiz){
$count = $quiz->where('is_correct_answer',1)->count()
}
}
@endphp
<td>{{$count}}</td>
</tr>
@endforeach
But of course you can write a method in a model to retrieve the correct answers. 但是,您当然可以在模型中编写一种方法来检索正确的答案。 and just call that method instead.
而是改为调用该方法。 you can write a method like this in User model:
您可以在用户模型中编写如下方法:
public function get_correct_answers($user_id){
$user = User::whereId($user_id)->first();
foreach($user->submittedAnswers()->get() as $answer){
foreach($answer->examQuizAnswers->get() as $quiz){
$count = $quiz->where('is_correct_answer',1)->count()
}
}
return $count;
}
And then in the view u just call that method like this: 然后在视图中,您只需像下面这样调用该方法:
<tr>
<th>Name</th>
<th>Answered</th>
<th>Correct</th>
</tr>
@foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{count($user->submittedAnswers()->get())}}</td>
<td>{{$user->get_correct_answers($user->id)}}</td>
</tr>
@endforeach
Loop your $users and echo the 3 fields you want. 循环$ users并回显您想要的3个字段。 Something roughly like this:
大概是这样的:
{{ $user->name }}
{{ $user->submittedAnswers->examQuizAnswers()->where('is_correct_answer', 1)->get()->count() }}
{{ $user->submittedAnswers->examQuizAnswers->count() }}
But please don't actually query in view files for the sake of the children. 但是请不要真正为了孩子而在视图文件中查询。
Eager load related models in your controller: 控制器中与负载有关的模型:
User::with(['submitted_answers', 'submitted_answers.exam_quiz_answers'])->get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.