为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
心有法竹 2021-12-03 16:27:02
我有两个不同的表,答案表和问题表,我需要获得我设法检索的用户选择的问题 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 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

这是我如何查询我的两个表数据


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

                          }


                          }


查看完整回答
反对 回复 2021-12-03
?
弑天下

TA贡献1818条经验 获得超8个赞

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


首先检查答案表有question_id列


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


function answers(){

  return $this->hasMany(\App\Answer::class);

}

所以现在你可以很容易地从问题模型的一个实例中说


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

dd($question->answer);

你现在会看到当你转储它时,它会给你属于这个问题的答案


查看完整回答
反对 回复 2021-12-03
  • 2 回答
  • 0 关注
  • 166 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信