首页 > 解决方案 > 我需要从两个不同的表中检索数据

问题描述

我有两个不同的表,答案表和问题表,我需要获取我设法检索的用户选择的问题 ID 和答案,我的问题来自同一个表单视图,我需要检索正确答案和要点我在问题表中创建。从我看来,前两列是从答案表中检索的,因此我需要从问题表中检索后两列,请任何可以帮助我的人,以下是我尝试做的事情

这是我的看法:

<table class="table table-hover">
    <thead class="thead-dark">
    <tr>
        <th scope="col">Question No</th>
        <th scope="col">Answer</th>
        <th scope="col">Correct Answer</th>
        <th scope="col">Marks</th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; ?>
    @foreach($results as  $data)
        <tr>
            <td>{{$i++}}</td>
            <td>{{$data->question_id}}</td>
            <td>{{$data->answer}}
            <td>{{$data->qns_ans}}
            <td>{{$data->qns_point}}
        </tr>
    @endforeach
    </tbody>
</table>

这是我的控制器

public function index()
{
    $results = Answers::all();
    $qns = Questions::all();

    return view('test.result')
        ->with('results', $results)
        ->with('qns', $qns);
}

标签: phplaravellaravel-5eloquent

解决方案


这是我查询两个表数据的方法

          This is my controller 
            public function index()
            {

            $results=Answers::all();
            $qns = Questions::all();

                 return view('test.result')->with('results',$results)
                             ->with('qns',$qns);
                 }

这是我的看法

                  <table class="table table-hover">
                    <thead class="thead-dark">
                        <tr>
                           <th>S/N</th>
                         <th scope="col">Question No</th>
                         <th scope="col">Answer</th>
                          <th scope="col">Correct Answer</th>
                           <th scope="col">Marks</th>

                             </tr>
                           </thead>
                           <tbody>
                               <?php $i=1; ?>
                                @foreach($results as  $data)
                             <tr>

                                 <td>{{$i++}}</td>
                                 <td>{{$data->question_id}}</td>
                                 <td>{{$data->answer}} </td>
                                 <td>{{$data->question['ans']}} </td>
                                 <td>{{$data->question['point']}} 

                              </tr>
                                  @endforeach
                                     </tbody>
                                 </table>

                      Here is my model
                     class Answers extends Model

                       {
                       protected $fillable = ['question_id','answer'];


                          public function question(){
                          return $this->belongsTo(Questions::class); 
                          }
                          }


                   class Questions extends Model
                            {
                         protected $fillable = 
                         ['question_name','opt1','opt2','opt3',
                          'opt4','ans','point'];

                          public function answer(){
                          return $this->hasOne(Answer::class); 
                          }

                          }

推荐阅读