简体   繁体   English

如何合并具有两个user_id的表并在视图中显示

[英]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.

相关问题 我想显示评论所有者的用户名,但是评论表仅具有user_id - I want to display username of comment's owner, however, comments table has user_id only 如何从此数组格式打印 user_Id? - how can I print user_Id from this array format? 如何在 laravel 中将用户 ID append 到 URL 的末尾? - How can I append a user_id to the end of a URL in laravel? 如何从附加到特定user_id的数据透视表中获取记录到其他表中 - how do I get records from pivot table attached to specific user_id into other table 如果刀片中的联接表中有user_id字段,如何获取用户名 - How to get the user name if I have user_id field in a joined table in a blade 如果user_id和post_id相同,如何从数据库中删除一个赞? - How can I delete a like from my database if user_id and post_id are the same? 如何添加显示“ $ user_id已删除”或“找不到$ user_id”的消息? - How would I add a message that says “$user_id Deleted” or “$user_id not found?” 我如何获取id和ip_address并在Codeigniter的ci_session表中添加一列user_id并添加一 - How can I get the id and ip_address and add one more column user_id in ci_session table in Codeigniter and add one Laravel 6:如何将 user_id 和 role_id(s) 发送到控制器,以同步 user_role 数据透视表? - Laravel 6: how do I send user_id with role_id(s) to controller, in order to sync user_role pivot table? 如何在其他表中输入user_id(laravel 5.2) - How to input user_id in different table (laravel 5.2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM