显示每个用户的总答案和正确答案

I have a db structure like this:

Tables:

users(id, email, password, ...) //default laravel users table

examinees(id, user_id, ...)

exam_quizzes(id, title, explanation)

exam_quiz_answers(id, title, exam_quiz_id, is_correct_ans)

submitted_answers(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:

<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:

<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. 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()