User model:
public function getFullName()
{
return "{$this->first_name} {$this->surname}";
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi guys,
I am open for any ideas or solutions, I wanted to search data from the database by full name but first name and last name is in different columns inside the table.
For example i search Ryan Angelo,
In database:
firstname - Ryan
lastname - Angelo
Thanks!
For multi-column search, I typically do something like this:
function scopeWithName($query, $name)
{
// Split each Name by Spaces
$names = explode(" ", $name);
// Search each Name Field for any specified Name
return User::where(function($query) use ($names) {
$query->whereIn('first_name', $names);
$query->orWhere(function($query) use ($names) {
$query->whereIn('last_name', $names);
});
});
}
Then you can just call $user->withName($name)->get() or User::withName($name)->get().
Note: If you support spaces in your name fields, this will not work. You'll probably want to convert this to some sort of Regex. However, the structure of the solution I've given still applies.
Another Note: This solution does not consider name ordering. The query "Angelo Ryan" will find any Users named "Ryan Angelo".
Please or to participate in this conversation.