How to Concat Two Columns in Laravel Example

Laravel CONCAT two columns example; In this tutorial you will learn how to concatenate Two Columns in Laravel 9 application. Laravel provides us the accessor and mutator to get and set values to concatenate two or more columns using model binding. We can also use the db raw query with select or query builder concat in where clause to merge two column in laravel.

This tutorial show you three different example to concat two columns in laravel. You can use these examples to concate columns in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

Example 1: Using Accessor Attribute

The Accessor attribute example is the easiest and sweetest things to adding two columns in laravel. You just need to add getColumnNameAttribute, attibute in your model.

An accessor transforms an Eloquent attribute value when it is accessed. To define an accessor, create a get{Attribute}Attribute method on your model where {Attribute} is the “studly” cased name of the column you wish to access.

In this example, we have first name and last name and we want to get a full name of user sho we can use this directally in blade file; So getting full name of all user you just need to add getFullNameAttribute]. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the full_name attribute.

If the full_name column in not in users table then you need to add column to the model object without adding in database table just like below:

protected $columns = ['full_name'];

Let’s open your app\Models\User.php file and update the following code just like below;

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
    * 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 in Blade File:

In blade file you don’t need anything just use $user->full_name and the full name will show here.

<table class="table table-bordered">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
        <th>Full Name</th>
        <th width="280px">Actions</th>
    </tr>
    @foreach ($users as $user)
        <tr>
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
            <td>{{ $user->full_name }}</td> //show your full name here
        </tr>
    @endforeach
</table>

Example 2: Wrap Query in DB::raw

You can use the DB::raw query for concat two columns easily. You just need to use select * and use the raw just like below.

In Controller:

public function index()
{
    $users = User::select("*", DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name"))->get();

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

in Blade:

Same as in mutator you don’t need anything else just use the full_name as well.

<table class="table table-bordered">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
        <th>Full Name</th>
        <th width="280px">Actions</th>
    </tr>
    @foreach ($users as $user)
        <tr>
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
            <td>{{ $user->full_name }}</td>
            <td>
                <button class="btn btn-danger" onclick="deleteConfirmation({{$user->id}})">Delete</button>
            </td>
        </tr>
    @endforeach
</table>

Example 3: Using Pluck Method

We can use the pluck() method to concat two columns like this:

public function index()
{
    $users = User::select("id", DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name"))
            ->pluck('full_name', 'id');
    dd($users);
}

So, today you learn how to concate two or more columns in laravel application. I hope you enjoy with this..

Leave a Comment