@hellopeter13
Assuming $request->subjects is an array, maybe something like this:
$student = Student::select('*')->where(function ($query) use ($request) {
if (!empty($request->location)) {
$query->whereIn('location', $request->location);
} else if (!empty($request->gender)) {
$query->where('gender', $request->gender);
} else if (!empty($request->subjects)) {
$placeholders = implode(',', array_fill(0, count($request->subjects), '?'));
$query->whereRaw(
"id in (select student_id from student_subjects where subject_id in ($placeholders)",
$request->subjets);
})->get();
You could use implode(',' $request->subjects) instead of placeholders but that's a bad idea since that's user input and you would be open to sql injection when using the whereRaw() method
To prevent that, you have to use bindings and in order to work , you need as many '?' as elements the $request->subject array has
For example, if the $request->subject is [201,205] then the string for the whereRaw() clause has to be whereRaw( "id in (select student_id from student_subjects where subject_id in (?,?)",[201,205])
I hope that makes sense
BTW, are these filters mutually exclusive ? Because the way the where clause in the Student model is written that's what is happening.