I'm trying to build a simple form based query system. It'll show all the queries and their responses to the user.
In controller, I'm returning questions model where user ID is Auth::id()
.
Here's my schema:
| id | user_id | lesson_name | statement | statement_type |
|--- | ------- | ----------- | ------------------ | -------------- |
| 1 | 1 | python | Any question asked | question |
| 2 | 1 | python | Response by admin | response |
I want to show all the entries related to a user for a particular lesson. Here's what I've done till now:
In the controller
$queries = Question::where('user_id', Auth::id())->get()->groupBy('lesson_name');
In blade file
@foreach ($queries as $query)
<div >
<a href="#" ><img src="{{ asset('img/signup.jpg') }}"
></a>
<div onclick="openQueryPopup()">
<h2 >{{ $query->value('lesson_name') }}</h2>
<p >Query message will show up here...</p>
<p >Sangam replied</p>
</div>
<div >
<a href="#" >Reply <i ></i>
</a>
</div>
</div>
<div id="query-container" >
<h4>
Your query about
<br>
Lesson name will appear here
</h4>
<div >
@foreach ($query->pluck('statement_type') as $statementtype)
<p >{{ $statementtype }}:</p>
<p >{{ $query->value('statement') }}</p>
@endforeach
</div>
<div >
<a href="#" >Reply <i ></i>
</a>
</div>
</div>
<div id="pseudo-container" onclick="closeQueryPopup()"></div>
@endforeach
I'm not able to fetch statement_type
and statement
at the same time.
Any help is appreciated.
Edit 1
I simply want to loop all statements of auth()->user()
with headings of statement_type
CodePudding user response:
If I understand correctly, you need a subquery inside your query to get current lesson's statement data (or messages, whatever it is)
I assumed your table name is questions
@foreach ($queries as $query)
..
@php
$subqueries = Question::where('user_id', Auth::id())->where('lesson', $query->value('lesson_name'))->get();
@endphp
..
@foreach ($sub_queries as $sub_query)
<p >{{ $sub_query->statement_type }}:</p>
<p >{{ $sub_query->statement }}</p>
@endforeach
..
If you need a correction just let me know
CodePudding user response:
I solved this issue by simply using collect
Here's what I did with $queries
variable:
$queries = Question::where('user_id', Auth::id())->get()- >groupBy('lesson_name');
$questions = collect($queries)->groupBy('lesson_name');
$questions->values()->all();
In Blade file
@foreach ($question as $statementtype)
<p >{{ $statementtype->statement_type }}:</p>
<p >{{ $statementtype->statement }}</p>
@endforeach