我需要从两个不同的表中检索数据

时间:2019-08-23 15:30:20

标签: php laravel laravel-5 eloquent

我有两个不同的表,答案表和问题表,我需要获取问题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);
}

2 个答案:

答案 0 :(得分:0)

首先,您需要在问题和答案之间建立联系one to many

首先检查答案表是否有question_id

然后将此方法添加到问题模型中以建立联系

function answers(){
  return $this->hasMany(\App\Answer::class);
}

现在,您可以轻松地从问题模型的实例中说出

$question = Question::first()->get();
dd($question->answer);

当您转储它时,您现在会看到,它将为您提供属于该问题的答案

答案 1 :(得分:0)

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

          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); 
                          }

                          }
相关问题