Larawel orWhere query from concat two column

Muhammad Ilham :

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

TsaiKoga :

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 . '%');

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=394867&siteId=1