php - 我需要从两个不同的表中检索数据
问题描述
我有两个不同的表,答案表和问题表,我需要获取我设法检索的用户选择的问题 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);
}
解决方案
这是我查询两个表数据的方法
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);
}
}
推荐阅读
- reactjs - 如何在 Material-UI 中使用 Box 组件覆盖按钮?
- dart - 如何在 Dart 中检查动态变量的 null
- javascript - 我尝试从 slack 获取信息并使用 node-fetch 获取数据
- reactjs - 如何在 React 中设置我的背景图像适合屏幕
- javascript - 西蒙说游戏的按钮响应问题
- encryption - 从媒体特别是音频生成 xml 清单
- ruby - 将字符串转换为 Float::INFINITY
- sap-cloud-sdk - 如何记录 ODataQueryBuilder API / VDM API 发送的 HTTP 请求?
- c++ - LNK1104 无法打开文件 'k4a::k4a.lib'
- php - 如何使用带有 Azure 存储帐户的 Azure 文件中的 PHP 显示文本内容本身?