Skip to content
Advertisement

where and orwhere is not working on eloquent

$classes = Classes::all();
if (!empty($request) and $request->search != null)
    $classes = $classes->where('class_name', 'like', '%' . $request->search . '%')
        ->orWhere('class_number', 'like', '%' . $request->search . '%');

Here both where and orWhere are not working in eloquent says Bad Method call. can anyone help me with this?

Advertisement

Answer

$classes is currently a Collection, not a QueryBuilder object. Add the full query to the if block and move the default to an else, like this:

if (!empty($request) and $request->search != null) {
    $classes = Classes::where('class_name', 'like', '%' . $request->search . '%')
        ->orWhere('class_number', 'like', '%' . $request->search . '%')
        ->get();
} else {
    $classes = Classes::all();
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement