I want to load all unique subjects like in the database. and Each Subject contains multiple questions and each question contains multiple answers. I do not whether this process is correct or not but with the process, .i got only one subject with multiple questions and answers. I hope you understand my problem. There are exams_answers database where I want to view the result of all subject -> questions ->answers by the same user_id and exam_id.
This is my database of ExamAnswer
This is my Controller File where I want to get the result in a blade file like this: There are two subject_id 1 & 2. I want to get two subjects and each subject contain questions and each question contain the answers.
public function result($id)
{
$results = ExamAnswer::where('exam_id', $id)->with('subject')->get();
foreach ($results as $result){
$subjects[] = $result->subject;
$object = collect($subjects);
$uniqueSubject = $object->unique();
$uniqueSubject->values()->all();
}
$allSubject = $uniqueSubject->all();
return view('frontend.mock.result',compact('results','allSubject));
}
This is my ExamAnswer Model
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
This is Subject Model
public function results(){
return $this->belongsTo('App\Result','subject_id');
}
This is my blade file. I want to load all unique subjects like in the database there are two ids 1 and 2. and Each Subject contains multiple questions and each question contains multiple answers.
<div>
@foreach($allSubject as $index => $subject)
@php $result=\App\ExamAnswer::where('exam_id', $exam->id)-
>where('subject_id',$subject->id)->with('question')->with('question.answers')-
>get();
@endphp
@foreach($result as $index=> $result)
<span>) {!! $result->question->question_body !!} </span>
@foreach($result->question->answers as $index => $answer)
) {!! $answer->answer_body !!}
@endforeach
@endforeach
</div>
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3jlHvmJ
via IFTTT
Aucun commentaire:
Enregistrer un commentaire