doukun5339 2018-01-19 09:50
浏览 72
已采纳

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

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
  • 写回答

2条回答 默认 最新

  • dougu2006 2018-01-19 10:33
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作