Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jayrec's avatar

Multiple select clauses not working properly in DB query

Hi, I'm running a query across 2 tables with unexpected results. When I use the following lines of code, the result ignores the clause "where users.userType = Student"

$searchword = Input::get ('q');
$student = \DB::table('userprofiles')->join('users', 'users.userName', '=', 'userprofiles.userName')->select('userprofiles.id','userprofiles.userName', 'userprofiles.fName', 'userprofiles.lName')->where('users.userType', '=', "Student")->where('userprofiles.userName', 'LIKE', '%' . $searchword . '%')->orwhere('userprofiles.fName', 'LIKE', '%' . $searchword . '%')->orwhere('userprofiles.lName', 'LIKE', '%' . $searchword . '%')->get();

I tried another method I found online:

$student = \DB::table('userprofiles')->join('users', 'users.userName', '=', 'userprofiles.userName')->select('userprofiles.id','userprofiles.userName', 'userprofiles.fName', 'userprofiles.lName')->where('users.userType', '=', "Student")->where(function($word){$word->where('userprofiles.userName', 'LIKE', '%' . $searchword . '%')->orwhere('userprofiles.fName', 'LIKE', '%' . $searchword . '%')->orwhere('userprofiles.lName', 'LIKE', '%' . $searchword . '%')})->get();

Then I get: FatalThrowableError Parse error: syntax error, unexpected '}'

What am I doing wrong?

Thanks.

0 likes
6 replies
hxd's avatar

edit this code: '->orwhere('userprofiles.lName', 'LIKE', '%' . $searchword . '%')})->get();' to '->orwhere('userprofiles.lName', 'LIKE', '%' . $searchword . '%'); })->get();', you forget a ‘;’ before '}'.

1 like
jayrec's avatar

When I try adding the ';', I get an ErrorException Undefined variable: searchword

Also, how do I properly format the code in laracasts? Cos it keeps reverting to plaintext.

Thanks.

jayrec's avatar

I added an if condition ($user->userType == "Student") in the view and it worked.

Please or to participate in this conversation.