[英]How can I combine a table that has two user_id and display in a view
I have an view issue in Laravel 我在Laravel有一个观点问题
Let's say 比方说
User
id|user_name|
1 |Amy|
2 |Bob|
3 |Candy|
4 |Dina|
5 |Edwin|
Product
id|product_name|
1 |iPhone 10|
2 |Galaxy 10|
3 |Huawei P10|
4 |Vivo R10|
5 |Xiaomi 10|
Q&A
id| Question | Answer |ask_user_id|ans_user_id|Product_id|
1 |Question 1|Answer For Q1| 5 | 1 | 1 |
2 |Question 2|Answer For Q2| 4 | 2 | 3 |
3 |Question 3|Answer For Q3| 3 | 4 | 3 |
4 |Question 4|Answer For Q4| 2 | 4 | 4 |
5 |Question 5|Answer For Q5| 1 | 5 | 5 |
I want to get the users for asking question and the users for answering the question. 我想让用户提出问题,并让用户回答问题。
Thus the control is: 因此,控件为:
public function getProductDetail($id) {
/*
Get Questions and Answers Q&A section
*/
//Get questions here
$sql = "SELECT USER.user_name as askUser, QA.question as theQuestion, QA.date_of_post FROM USER, QA WHERE id IN (SELECT ask_user_id FROM QA WHERE product_id = ?) AND USER.id = QA.ask_user_id";
$questions = DB::select($sql, array($id));
//Get answers here
$sql = "SELECT USER.user_name as ansUser, QA.answer FROM USER, QA WHERE id IN (SELECT ans_user_id FROM QA WHERE product_id = ?)AND USER.id = QA.ans_user_id";
$answers = DB::select($sql, array($id));
return view('includes.products.productDetail',
['questions' => $questions, 'answers' => $answers]);
}
In the view, I wrote the code like: 在视图中,我编写了如下代码:
<div class="QA">
<h2>Questions and Answers</h2>
<div class="question">
<ul class="list-unstyled">
@foreach($questions as $question)
<div class="shadow p-3 mb-5 bg-white rounded">
<li>{{ $question -> theQuestion }}</li>
<li>{{ $question -> askUser }} asked on {{ $question -> date_of_post}}</li>
</div>
@endforeach
@foreach($answers as $answer)
<li>{{ $answer -> answer }}</li>
<li>{{ $answer -> ansUser}}</li>
@endforeach
</ul>
</div>
</div>
I have tried in different ways, but the question and results always display separately. 我尝试了不同的方法,但是问题和结果始终分开显示。 I want them to be shown in the format like:
我希望它们以以下格式显示:
Q&A Q&A
Question 1
author
Answer section
ansUser1 answer
ansUser2 answer
_________________
Question 2
author
Answer section
ansUser3 answer
ansUser4 answer
_________________
Question 3
author
Answer section
ansUser2 answer
ansUser3 answer
I am not sure if the problem comes from my view or from SQL. 我不确定问题是来自我的观点还是来自SQL。 Appreciate!!
欣赏!!
One way to achieve that is to loop all answers for each question, then check if the answer matches the question. 一种实现方法是循环每个问题的所有答案,然后检查答案是否与问题匹配。 (not the best way, but still, will work)
(不是最好的方法,但是仍然可以)
To do that, get the question id in the answer Query 为此,请在答案查询中获取问题ID
$sql = "SELECT USER.user_name as ansUser, QA.answer, QA.question, FROM USER, QA WHERE id IN (SELECT ans_user_id FROM QA WHERE product_id = ?)AND USER.id = QA.ans_user_id";
Then change the foreach loop 然后更改foreach循环
@foreach($questions as $question)
<div class="shadow p-3 mb-5 bg-white rounded">
<li>{{ $question -> theQuestion }}</li>
<li>{{ $question -> askUser }} asked on {{ $question -> date_of_post}}</li>
@foreach($answers as $answer)
@if($answer->question == $question->theQuestion)
<li>{{ $answer -> answer }}</li>
<li>{{ $answer -> ansUser}}</li>
@endif
@endforeach
</div>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.