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

jhutto's avatar

Multiple Where statements not working.

I'm passing multiple id's to a controller and trying to have it query the database. I have 3 tables that are linked in the query classsemester somascourses instructors

This mysql statement will retrieve the information from the database correctly. SELECT * FROM classsemester, somascourses, instructors WHERE classsemester.id = somascourses.SemesterID AND somascourses.Instructor_id = instructors.id AND classsemester.id = 2 AND somascourses.id = 2

Here's the function in the edit controller...

Since all the tables have a primary key of id.... I had to change the primary key for somascourses.id to course_id for it to show up in cc($course)l=; I need the to be able to put the starting table of classsemesters by the $id.. ID for that table and then pull the somascourses by a specific is using $courseid. Because I trying to pull a specific course from somascourses to edit.

 public function edit($id, $courseid)
    {
        
$course = DB::table('classsemester')
            ->join('somascourses', 'classsemester.id', '=', 'somascourses.SemesterID')    
            ->select('*','somascourses.id as course_id')
            ->join('Instructors', 'somascourses.Instructor_id', '=', 'Instructors.id')
            ->where('classsemester.id', '=', $id)
            ->orwhere('somascourses.SemesterID','=', $id)
            ->where('course_id', '=', $courseid)
            ->get();

}

How do I write this so, it will pull the same information as this mysql statement. SELECT * FROM classsemester, somascourses, instructors WHERE classsemester.id = somascourses.SemesterID AND somascourses.Instructor_id = instructors.id AND classsemester.id = 2 AND somascourses.id = 2

0 likes
3 replies
cmdobueno's avatar

You must group them


$course = DB::table('classemester')
    ->join('somascourses', 'classsemester.id', '=', 'somascourses.SemesterID')    
    ->select('*','somascourses.id as course_id')
    ->join('Instructors', 'somascourses.Instructor_id', '=', 'Instructors.id')
    ->where(function($query) use($id){
        $query->where('classsemester.id', '=', $id)
                        ->orwhere('somascourses.SemesterID','=', $id)
    })
    ->where("course_id","=",$courseid)
    ->get();

Maybe this will help. It provides a grouped where clause.

jhutto's avatar

Sorry... It looks right but keep getting

syntax error, unexpected '}'

I've tried multiple variations.

Please or to participate in this conversation.