How to Concat First Name and Last Name in Laravel?

By Hardik Savani April 16, 2024 Category : 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...

Tags :
Shares