title: Select from multiple tables with laravel
body: <p>I have the following MySql query:</p>
  
  <pre><code>SELECT DISTINCT students.fname, students.lname, students.year_level 
  FROM (students, teachers) 
  JOIN teacher_subjects ON teachers.teacherID = teacher_subjects.teacherID 
  JOIN subjects ON teacher_subjects.subjectID = subjects.subjectID 
  JOIN teacher_sections ON teachers.teacherID = teacher_sections.teacherID 
  JOIN student_subjects ON students.studentID = student_subjects.subjectID 
  JOIN sections ON students.sectionID = sections.sectionID 
  WHERE teacher_sections.sectionID = students.sectionID 
        AND student_subjects.subjectID = teacher_subjects.subjectID
  </code></pre>
  
  <p>Anyone have an idea of how to convert this into an Laravel's query builder? Thanks.</p>
  
  <p>I tried this, but it doesn't work</p>
  
  <pre><code>$student = DB::table('students', 'teachers')
                          -&gt;join('teacher_subjects','teachers.teacherID', '=', 'teacher_subjects.teacherID')
                          -&gt;join('subjects','teacher_subjects.subjectID', '=', 'subjects.subjectID')
                          -&gt;join('teacher_sections','teachers.teacherID', '=', 'teacher_sections.teacherID')
                          -&gt;join('student_subjects','students.studentID', '=', 'student_subjects.subjectID')
                          -&gt;join('sections','students.sectionID','=','sections.sectionID')
                          -&gt;select('students.fname', 'students.lname', 'students.year_level')
                          -&gt;where([
                              ['teacher_sections.sectionID', '=', 'student.sectionID'],
                              ['student_subjects.subjectID', '=', 'teacher_subjects.subjectID']
                              ])
                          -&gt;distinct()
                          -&gt;get();
                          return view('teacher_class', compact('student'));
  </code></pre>