ethar
1 month ago

return last record by forign_key

Posted 1 month ago by ethar

i have chapters table, student take exams ,

in student_exams i store ('id','chapter_id','mark','user_id')

I want to list all chapters and marks of last taken exam related to chapter.

i try this code


            $chapter = Chapter::select('id', 'title_ar')
                ->with(['studentExams' => function ($q) {
                    $q->select('chapter_id', 'mark')
                        ->where('student_id', Auth::user()->id)
                        ->orderby('id', 'DESC')
                        ->first();
                }])->get();

but it returns the last exam is taken, not the last exam in the selected chapter.

Please sign in or create an account to participate in this conversation.