$users = DB::table('users')->select("*", DB::raw("CONCAT(users.first_name,' ',users.last_name) AS full_name"))
->get();
//must have
use IlluminateSupportFacadesDB;
Model::select(DB::Raw("CONCAT(profiles.first_name, ' ', profiles.last_name) AS name"))->get();
//concat other table data and display as one line with ',' as a delimiter
//DB::Raw("(SELECT GROUP_CONCAT(a.contact_number) FROM contacts as a JOIN users as b ON a.user_id = b.user_id WHERE a.user_id = users.user_id) AS contact")
//expected output would be 09123456789,09876543210,09000000000
public function scopeFindUserByName($query,$name) {
// Concat the name columns and then apply search query on full name
$query->where(DB::raw(
// REPLACE will remove the double white space with single (As defined)
"REPLACE(
/* CONCAT will concat the columns with defined separator */
CONCAT(
/* COALESCE operator will handle NUll values as defined value. */
COALESCE(name_first,''),' ',
COALESCE(name_middle,''),' ',
COALESCE(name_last,'')
),
' ',' ')"
),
'like', '%' . $name . '%');
}