How to Concat First Name and Last Name in Laravel?
Hi,
I am going to explain you example of how to concat first name and last name in laravel. you will learn concat first name and last name in laravel. you will learn laravel concat first name and last name. you will learn laravel concatenate first name middle name last name example.
Here, i will give you simple examples of how to concat first name and last name using laravel eloquent query builder. i will give you two example to contact user first name, middle name and last name in laravel. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
let's see bellow example:
Example 1: Concat First Name and Last Name
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()
{
$users = User::select("id", DB::raw("CONCAT(first_name, ' ', last_name) as full_name"))
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 126
[full_name] => Hardik Raiyani
)
[1] => Array
(
[id] => 127
[full_name] => Hardik Savani
)
)
Example 2: Concat First Name, Middle Name and Last Name
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()
{
$users = User::select("id", DB::raw("CONCAT(first_name, ' ', middle_name, ' ', last_name) as full_name"))
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 126
[full_name] => Hardik Manubhai Raiyani
)
[1] => Array
(
[id] => 127
[full_name] => Hardik Manubhai 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 Min Value Query Example
- 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 inRandomOrder() Method Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial