Laravel nestedwhere Parse error: syntax error, unexpected '}'

297 Views Asked by At

Symfony\Component\Debug\Exception\FatalThrowableError: Parse error: syntax error, unexpected '}'

I followed this resources for nested where here: How to combine WHERE clauses in Eloquent but get the following error while running the codes. The line where it broke was the curly bracket of the callback function, what's wrong here?

$providers = DB::table('users')
->where('verified', '=', 1)
->where('status', '=', 1)
->where(function ($query) use ($search) {
    $query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
    ->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%")
})
->where('city', '=', $t)
->take($limit)
->toSql();
1

There are 1 best solutions below

2
Virginia On BEST ANSWER

You are missing a ;after your inner $query. I think that part could be this:

->where(function ($query) use ($search) {
    $query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
    ->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%");
})

The code needs that semicolon at the end, because it is a line / statement in a function. It does not matter that this function is a callback.