Skip to content
Advertisement

Laravel – query to select with “with” and “where” clause

I have the following:

public function filterStudentsByDateOfInterview($interview_date)
{
    //return students with the day of interview:

     $students = Students::with('interviews')
         ->where('interviews.interview_date', $interview_date) //this returns me an error
         ->get();

    return $students;
}

my question is the following: how can I make the where statement on the interviews column? if I don’t pass the where statement, works. But I need to get only the data where the date is equal to my param of the function.

The error it returns me:

Unknown column 'interviews.interview_date' in 'where clause'

Thanks in advance!

Advertisement

Answer

you can pass callaback function like that

public function filterStudentsByDateOfInterview($interview_date)
{
    //return students with the day of interview:

     $students = Students::whereHas('interviews' ,function($query) use($interview_date){
        $query->where('interview_date', $interview_date);
     })->get();
         

    return $students;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement