Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maneeshpatel's avatar

Triple foreign key BelongsToMany Relations returns null in Laravel

I am implementing pivot table with multiple foreign keys Laravel 5 Eloquent. Already read these links-

My tables are as below:-

Tables-

Quizzes
-Id
-Name

Sections
-Id
-Name

Quiz_Section
-Section_id
-Quiz_Id
-Max_Marks
-Max_Questions

Questions
-Id
-Description

Quiz_Section_Question
-Quiz_id
-Section_Id
-Question_Id

Modal-

Quiz Modal

public function sections()
{
    return $this->belongsToMany('App\Section', 'quiz_section', 'quiz_id', 'section_id')->withPivot('max_questions','max_marks');
}

public function sectionsWithName()
{
    return $this->hasMany('App\QuizSection','quiz_id', 'id')
    ->join('sections', 'quiz_section.section_id', '=', 'sections.id')
    ->selectRaw('quiz_id, section_id, sections.name, max_questions, max_marks');
}   

Section Modal

public function quizzes()
{
    return $this->belongsToMany('App\Quiz', 'quiz_section', 'section_id', 'quiz_id')->withPivot('max_questions','max_marks');
}

public function questions()
{
    return $this->belongsToMany('App\Question','quiz_section_question', 'section_id','question_id')->withPivot('quiz_id');
}

QuizSection Modal

protected $table = 'quiz_section';

public function Quiz()
{
     return $this->belongsTo('App\Quiz');
}

public function questions()
{
    return $this->belongsToMany('App\Question','quiz_section_question', 'section_id','question_id')->withPivot('quiz_id');
}   

Now I want to get all sections with name and questions attached to those sections for a particular quiz.

SQL Query that will give desired output-

SELECT quizzes.name, sections.name, quiz_section.max_marks, quiz_section.max_questions,questions.description FROM quizzes 
INNER JOIN quiz_section ON quizzes.id = quiz_section.quiz_id
INNER JOIN sections ON sections.id = quiz_section.section_id
INNER JOIN quiz_section_question ON (quiz_section_question.quiz_id = quizzes.id AND quiz_section_question.section_id=sections.id)
INNER JOIN questions ON questions .id = quiz_section_question.question_id;

I want to do it with Eloquent and Relationships. I have tried below code:-

Tried Code-

 $quiz = \App\Quiz::with('sectionsWithName.questions')->where('name','practice paper')->first();
 dd($quiz->sectionsWithName[0]->questions()->toSql());

Output:-

Every thing is fine but Questions collection is null.

"select * from `questions` inner join `quiz_section_question` on `questions`.`id` = `quiz_section_question`.`question_id` where `quiz_section_question`.`section_id` is null"

Please give valuable suggestions.

Edit:-

"Section.Questions" relationship returns all questions.

 $quiz = \App\Quiz::with('sections.questions')->where('name','practice paper')->first();
 dd($quiz->sections[0]->questions()->toSql());

But I want to filter question on basis of Quiz & Sections both.

0 likes
0 replies

Please or to participate in this conversation.