Skip to content
Advertisement

Larawel orWhere query from concat two column

I have a users table: id | firstname | lastname when displaying users data, I have a filter name so a user can search by name (first, last or full name); the problem is when I input it fullname it result in 0 data, eg: fistname: michael lastname: foobar when i look for “michael” and “foobar” i have no problem, but when i look for “michael foobar” it return nothing

my code look like this:

$dataQuery  = $dataQuery->where(function ($dataQuery) use ($fullname) {
            $dataQuery->where('users.firstname', 'like', '%' . $fullname . '%')
                  ->orWhere('users.lastname', 'like', '%' . $fullname . '%')
                  ->orWhere('CONCAT_WS(" ", `users.firstname`, `users.lastname`)', 'LIKE', '%' . $fullname . '%');
        });

ps: I’m sorry for my lack of english, i hope y’all understand

Advertisement

Answer

You are using mysql in-built function CONCAT_WS, plz use DB::raw() for the function:

->orWhere(DB::raw('CONCAT_WS(" ", `users.firstname`, `users.lastname`)'), 'LIKE', '%' . $fullname . '%');
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement