Try this
$course_id = $this->course_id;
$students = $this->student->whereHas("course", function($query) use ($course_id) {
$query->where("l_courses.id", "LIKE", "%". $course_id ."%");
})->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello , I'm trying to fetch all of the students in the table if they have any course related to their id , other wise if there is no course id to select from return all the students . I have wrote this part
$students = $this->student->whereHas("course", function($query){
$query->where("l_courses.id", "LIKE", "%".$this->course_id."%");
})->get();
but this part of code retrieve just those students which has course if there were not any course id . any Clue ?
Please or to participate in this conversation.