简体   繁体   中英

How can I combine a table that has two user_id and display in a view

I have an view issue in 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

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. 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

$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($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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM