In this article, I will explain how to concat two columns in Laravel query. This post will provide an easy example of how to concatenate string in Laravel select query. You will understand the concept of Laravel concat columns. I have also explained how to use Laravel’s ‘DB::raw’ query.
1. Concat two columns in Laravel query using eloquent model
If you are using the eloquent ORM, you can use the ‘DB::raw’ method to concatenate columns in your select statement.
Example:
You have a ‘User’ model, and you want to concatenate the ‘first_name’ and ‘last_name’ columns.
<?php
use App\Models\User;
use Illuminate\Support\Facades\DB;
$users = User::select(
'id',
'email',
DB::raw("CONCAT(first_name, ' ', last_name) as full_name")
)->get();
foreach($users as $user) {
echo $user->full_name;
}
?>
Explanation:
- DB::raw allows us to write raw SQL expressions.
- CONCAT(first_name, ‘ ‘, last_name) as full_name combines the first_name and last_name columns with a space in between.
- The result is saved as full_name, which we can access like any other attribute on the $user object.
In this example ‘DB::raw’ is used to select a raw query that concatenates the ‘first_name’ and ‘last_name’ columns with a space between them.
Read also: How to get last inserted id in Laravel?
2. Concat two columns in Laravel using query builder
You can also use the query builder to concat two columns in Laravel query. I have provide an easy example below.
Example:
<?php
use Illuminate\Support\Facades\DB;
$users = DB::table('users')
->select(
'id',
'email',
DB::raw("CONCAT(first_name, ' ', last_name) as full_name")
)->get();
foreach($users as $user) {
echo $user->full_name;
}
?>
Explanation:
- Query doesn’t return model instances, so we access properties directly, like $user->full_name.
3. Concat two columns in Laravel using adding a custom accessor
If you need the concatenated two column in your requirement, you might want to add a custom accessor to your eloquent model. This allow you to define a new attribute that concatenates the two columns.
Example:
1. Add the accessor to your User model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Use extends Model {
public function getFullnameAttribute()
{
return "{$this->first_name} {$this->last_name};"
}
}
?>
2. Use the accessor in your queries:
<?php
$users = User::all();
foreach($users as $user) {
echo $user->full_name;
}
?>
Explanation:
- The getFullnameAttribute method creates a custom attribute called full_name.
- Laravel automatic recognizes it as an accessor and allow us to access $user->full_name.
4. Use pluck concat two columns:
we have explained of concat two columns in laravel using laravel pluck method. I have explain easy example below.
$users = User::select('id', DB::raw("CONCAT(users.first_name, ' ', users.last_name) as full_name"))
->pluck('full_name', 'id');
dd($users);
5. Access in blade file
@foreach($users as $user)
<h6>{{$user->full_name}}</h6>
@endforeach
5. Using Collection to concatenate
You might want to concatenate columns on a collection after retrieving records from the database.
$users = User::all()->map(function ($user) {
$user->full_name = "{$user->first_name} {$user->last_name}";
return $user;
});
foreach($users as $user) {
echo $user->full_name;
}
- map() allows you to iterate over each user in the collection and add a full_name property directly.
- This can be useful for dynamically adding concatenated attributes without modifying the model.
I hope this tutorial help you.