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 and laravel 8 version.

let’s see bellow example:

Example 1: Concat First Name and Last Name

Controller Code:

<?php

namespace AppHttpControllers;

use AppModelsUser;

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 AppHttpControllers;

use AppModelsUser;

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…

2 thoughts on “How to Concat First Name and Last Name in Laravel?”

Leave a Comment