I am trying to make a conditional where, inside a select statement. It is throwing undefined variable 'class_id'. What is going wrong here. The variable class_id got some value but is throwing 'Undefined variable: class_id'.
$class_id = isset($searchData[1]) ? $searchData[1] : '';
\Log::info($class_id);
$students = \User::where('role', 'student')
->where('fullName', 'like', '%'.$student_name.'%')
->orWhere('username', 'like', '%'.$student_name.'%')
->orWhere('email', 'like','%'.$student_name.'%')
->when($class_id != '',
function ($q) {
return $q->where('studentClass', '=', $class_id);
})
->get();