CoderTn
CoderTn

Reputation: 997

Search users by fullname in laravel

i want to search users by fullname so i need to concat two columns which are first_name and last_name and look if it matches the given value or not, this is my try

$users = User::where(function($usersSearchQuery) use ($user,$fullName){
$usersSearchQuery
->whereNotIn('id', $user->blockerUsers->pluck('blocked_id'))
->whereNotIn('id', $user->blockedUsers->pluck('blocker_id'))
->whereRaw('CONCAT(first_name," ",last_name) LIKE \''.$fullName.'\'%'); })->get();

i have an error in this line

->whereRaw('CONCAT(first_name," ",last_name) LIKE  \''.$fullName.'\'%');

message: "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1 (SQL: select * from users where (1 = 1 and 1 = 1 and CONCAT(first_name," ",last_name) LIKE 'BertHue'%))"

Upvotes: 0

Views: 410

Answers (1)

Anthony Aslangul
Anthony Aslangul

Reputation: 3847

Try this:

$users = User::where(function($usersSearchQuery) use ($user,$fullName){
            $usersSearchQuery->whereNotIn('id', $user->blockerUsers->pluck('blocked_id'))
            ->whereNotIn('id', $user->blockedUsers->pluck('blocker_id'))
            ->whereRaw('CONCAT(first_name," ",last_name) LIKE \''.$fullName.'%\'');
        })->get();

I replaced your last where with a whereRaw which allows you to use raw SQL.

Upvotes: 3

Related Questions