tariqul anik
tariqul anik

Reputation: 324

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?

Upvotes: 0

Views: 39

Answers (1)

aynber
aynber

Reputation: 23001

$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();
}

Upvotes: 4

Related Questions