ItSolutionStuff.com

How to Concat First Name and Last Name in Laravel?

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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Group By with Min Value Query Example

Read Now →

Laravel Group By with Max Value Query Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Search Case Insensitive Query Example

Read Now →

Laravel Case Sensitive Query Search Example

Read Now →

How to Select Custom Column with Value in Laravel Query?

Read Now →

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent whereNotBetween() Query Example

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →