Home > front end >  Get data from two different database columns
Get data from two different database columns

Time:09-10

i have tow column in my sql and use this query to search

$Person->where('name','LIKE',"%{$keyword}%")
                    ->orWhere('family_Name','LIKE',"%{$keyword}%")
                    ->orWhere('id',$keyword);

but if user type name and family name together then result is null how can i get where from sum of tow column? or any other way

CodePudding user response:

If I understood correctly, a raw sql command like the one below might work for you.

$person->where(DB::raw('CONCAT_WS(" ", name, family_Name)'), 'like', '%'.$keyword.'%')->get();

CodePudding user response:

I assume you want search "John Doe". You put "John" at column name and "Doe" at family_name.

So, you can add CONCAT_WS() for this.

$Person
   ->where('name','LIKE',"%{$keyword}%")
   ->orWhere('family_Name','LIKE',"%{$keyword}%")
   ->where(DB::raw('CONCAT_WS(" ", name, family_name)'), 'like', "%{$keyword}%") // <-- here
   ->orWhere('id',$keyword);
  • Related