ItSolutionStuff.com

How to Concat Two Columns in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hello Folks,

Today, I will let you know example of how to concat two columns in laravel. This post will give you a simple example of laravel concatenate string select query. you can understand a concept of laravel select concat column example. I would like to share with you laravel select db raw query.

I will give you two way solution for concatenate columns in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app. both way you can easily concatenating two columns in laravel 6 application.

First way is we will concat two columns in select statement using db raw in laravel 6. bellow i will give you full example, so you can easily use same code in your application. you can easily use with pluck method too.

Second way is we will use laravel model eloquent scope, using scope we will concat two columns. but i think we can say crop is a limited solution. But if you don't require more complex then you can use it. You can see both example bellow so. You will understand.

Example 1:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$users = User::select("users.id",

DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name"),

"users.email"

)

->get();

return view('home', compact('users'));

}

}

Use:

@foreach ($users as $user)

{{ $user->full_name }}

@endforeach

Example 2:

You need to set scope in your model like as we defining on user model.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable

{

use Notifiable;

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'first_name', 'last_name', 'email', 'password','type','is_active',

];

/**

* The attributes that should be hidden for arrays.

*

* @var array

*/

protected $hidden = [

'password', 'remember_token',

];

/**

* The attributes that should be cast to native types.

*

* @var array

*/

protected $casts = [

'email_verified_at' => 'datetime',

];

/**

* Get the user's full concatenated name.

* -- Must postfix the word 'Attribute' to the function name

*

* @return string

*/

public function getFullNameAttribute()

{

return "{$this->first_name} {$this->last_name}";

}

}

Use:

@foreach ($users as $user)

{{ $user->full_name }}

@endforeach

Use Pluck Concat Two Columns:

$users = User::select("id", DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name"))

->pluck('full_name', 'id');

dd($users);

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

How to Select Specific Columns in Laravel Eloquent Model?

Read Now →

Laravel Eloquent Group By with Month and Year Example

Read Now →

Laravel Eloquent Left Join Where Null Condition Example

Read Now →

Laravel Eloquent firstOr() Example

Read Now →

Laravel Eloquent withMin(), withMax() and withAvg() Example

Read Now →

Laravel Eloquent firstOrCreate Example

Read Now →

Laravel Eloquent Delete Record By ID Example

Read Now →

PHP Laravel Left Join Query Example

Read Now →

PHP Laravel Inner Join Query Example

Read Now →

Laravel Eloquent orderByRaw() Query Example

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Where Condition with Two Columns Example

Read Now →

Laravel Where Clause with date_format() Example

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →