2 回答
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);
}
}
TA贡献1818条经验 获得超8个赞
首先你需要one to many在问题和答案之间建立联系
首先检查答案表有question_id列
然后将此方法添加到您的问题模型中以建立关系
function answers(){
return $this->hasMany(\App\Answer::class);
}
所以现在你可以很容易地从问题模型的一个实例中说
$question = Question::first()->get();
dd($question->answer);
你现在会看到当你转储它时,它会给你属于这个问题的答案
- 2 回答
- 0 关注
- 166 浏览
添加回答
举报