How to Search First Name and Last Name in Laravel Query?
Hi,
In this tutorial we will go over the demonstration of how to search first name and last name in laravel. In this article, we will implement a laravel get user full name search. We will use laravel search by full name. this example will help you search first name and last name in laravel. you will do the following things for laravel first name and last name where condition.
Mostly we take two columns first name and last name for full name. but when you need to add one input box and search full name then we have to think about it because we taken two separate columns. But not worry about it, i will give you very simple example how to search first name and last name in laravel query builder using orWhere() and DB.
Here, i will give you three examples that you can use with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Example 1:
Controller Code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use DB;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$search = "Hardik Sa";
$users = User::select("id", "first_name", "last_name")
->orWhere(DB::raw("concat(first_name, ' ', last_name)"), 'LIKE', "%".$search."%")
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 127
[first_name] => Hardik
[last_name] => Savani
)
)
Example 2:
Controller Code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$search = "Hardik Sa";
$users = User::select("id", "first_name", "last_name")
->whereRaw("concat(first_name, ' ', last_name) like '%" .$search. "%' ")
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 127
[first_name] => Hardik
[last_name] => Savani
)
)
Example 3:
Controller Code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$search = "Sa";
$users = User::select("id", "first_name", "last_name")
->where(function ($q) use ($search) {
$q->orWhere('first_name', 'like', "%{$search}%")
->orWhere('last_name', 'like', "%{$search}%");
})
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 127
[first_name] => Hardik
[last_name] => Savani
)
)
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Group By with Max Value Query Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Search Case Insensitive Query Example
- Laravel Case Sensitive Query Search Example
- How to Select Custom Column with Value in Laravel Query?
- Laravel Eloquent withMin(), withMax() and withAvg() Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial