Laravel Concatenation: Combining Two Columns with Examples

In this article, we will explore different ways to concatenate two columns in Laravel applications, specifically in versions 6, 7, 8, 9, and 10. Concatenating columns is useful when you want to display the combined result of two or more columns in a single query. We will cover two methods to achieve this: using Laravel…

By.

•

min read

In this article, we will explore different ways to concatenate two columns in Laravel applications, specifically in versions 6, 7, 8, 9, and 10.

Concatenating columns is useful when you want to display the combined result of two or more columns in a single query.

We will cover two methods to achieve this: using Laravel Query Builder with DB raw and using Laravel Eloquent Model Scope. Both methods have their advantages and limitations, so choose the one that suits your needs best.

Before we dive into the examples, let’s ensure that you have the necessary prerequisites in place. You should have a working Laravel application with a User model and a database connection.

Make sure you have the required columns in the database schema for this example, such as ‘first_name’ and ‘last_name’. Now, let’s get started with the two methods.

[lwptoc]

 

Method 1: Concatenating columns using Laravel Query Builder and DB Raw

The first method involves using the Laravel Query Builder and DB raw to concatenate two columns in a SELECT statement. This method provides a more flexible approach and can be used with the pluck method as well. Here’s the complete example:

public function showUsers()
{
    $userList = User::select("*", DB::raw("CONCAT(users.first_name, ' ', users.last_name) as full_name"))
        ->get();
  
    return view('userlist', compact('userList'));
}

 

In your view file, use the following code to display the full names of users:

@foreach ($userList as $userInfo)
    {{ $userInfo->full_name }}
@endforeach

 

Method 2: Concatenating columns using Laravel Eloquent Model Scope

The second method uses Laravel Model Eloquent Scope to concatenate two columns. This method is more limited compared to the first method but can be useful in less complex scenarios. To use this method, create a scope in your User model like this:

class UserModel extends Authenticatable
{
    // ...

    public function getCompleteNameAttribute()
    {
        return "{$this->first_name} {$this->last_name}";
    }
}

 

To display the full names of users, use the following code in your view file:

@foreach ($userList as $userInfo)
    {{ $userInfo->complete_name }}
@endforeach

You can also use the pluck method with this approach:

$userList = User::select("id", DB::raw("CONCAT(users.first_name, ' ', users.last_name) as complete_name"))
        ->pluck('complete_name', 'id');
  
dd($userList);   

 

Conclusion

In this article, we explored two ways to concatenate columns in Laravel applications. Both methods have their advantages, and the choice depends on your specific requirements. Make sure to choose the method that best suits your needs, and feel free to modify the examples to match your application’s structure.

Leave a Reply

Your email address will not be published. Required fields are marked *