Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhungund's avatar

Search first_name + last_name from one input tag

I'm displaying data related to different tables in table format in the view


-- | id | Employee Name | Project Worked on | Days| Feature Name | -- 

--- | txtBox | txtBox | txtBox | txtBox | txtBox | --
1. | 1 Delilah Will | Leave planner | 20 | Auto SMS 2. | 1 Delilah Will | Leave planner | 20 | Auto EMail
3. | 2 Randall Robert | Medical Store | 10 | Inventory

so each column has a input tag , which searches data from the related table ex : if i search based on project name : it will be searched in Projects table -> Project Name

When it comes to searching based on employee name there are two columns first_name and last_name in Employees table. So if i want to search based on employee name, it has to search like first_name and last_name is one single column. search can be done either on first_name or last_name or both

ex: Randall or Roberts or Randall Robert

0 likes
11 replies
Ionut's avatar

Add another "full_name" column ? :)

Limweb's avatar

use where input like concat('firstname','lastname')

2 likes
pmall's avatar
$models = YourModel::whereRaw('concat(\'first_name\', \'last_name\')', 'LIKE', '%' . $input . '%');
1 like
JarekTkaczyk's avatar

@rhungund I suggest something a bit longer than guys above. And you need a separator in the concat and obviously no quotation marks ' @pmall @Limweb:

select concat('first', 'last');
// 'firstlast'

select concat('first', ' ', 'last');
// or
select concat_ws(' ', 'first', 'last');
// 'first last'

The above of course assuming MySQL, but it's pretty similar in other servers.

Anyway, here's what I suggest:

Model::whereRaw("concat(first_name, ' ', last_name) like '%?%'", [$nameFromInput])
          ->orWhereRaw("concat(last_name, ' ', first_name) like '%?%'", [$nameFromInput])
          ->get();
3 likes
rhungund's avatar

thanks for the reply every one :)

I'm retrieving data like this..


foreach ($columns as $column) { 
    $query->orWhere($column, 'LIKE', '%' . $value . '%'); 
} 

where $column is table_name.column_name ex: employees.first_name, employee.last_name, projects.project_name, so on..

then how can i use concat(first_name, ' ', last_name) like '%?%'

JarekTkaczyk's avatar
Level 53

@rhungund Just the same:

->orWhereRaw("concat(first_name, ' ', last_name) like '%?%' ");

And take my advice and wrap all your orWheres in parentheses, like this:

$query->where(function ($q) use ($columns, $value) {
  foreach ($columns as $column) {
    $q->orWhere($column, 'like', "%{$value}%");
  }
});

Otherwise any and where clause and your whole query is messed up (SoftDelete scope being one for example).

And for your concat, this is what you need exactly:

$columns = ['col1', 'col2', ..., DB::raw("concat(first_name, ' ', last_name)")];
7 likes
davorminchorov's avatar

//search by first or last name Model::where('first_name', 'LIKE', '%$input%')->orWhere(('last_name', 'LIKE', '%$input%')->get(); // create a method to concatenate the result and show it in the view - this will probably be a view presenter. public function fullName($firstName, $lastname) { return $this->firstName + ' ' + $this->lastName; }
alexmansour's avatar

Hi @JarekTkaczyk

I have tried your solution for searching two first name and last name as one field full time.

Here is my code:

$customers = Customer::orderByRaw("concat(first_name, ' ', last_name)") ->whereRaw("concat(first_name, ' ', last_name) LIKE '%?%'", [$search]) ->orWhereRaw("concat(last_name, ' ', first_name) LIKE '%?%'", [$search]) ->orWhere('email', 'LIKE', '%' . $search . '%') ->orWhere('phone_number', 'LIKE', '%' . $search . '%') ->orWhere('address', 'LIKE', '%' . $search . '%') ->paginate(5);

Any suggestions ?

Thanks in advance.

eXist73's avatar

Thanks for the info.

I went with:

->orWhereRaw("concat(first_name, ' ', last_name) like '%?%' ");
1 like

Please or to participate in this conversation.