Laravel concat two columns with example
In this post we will give you information about Laravel concat two columns with example. Hear we will give you detail about Laravel concat two columns with exampleAnd how to use it also give you demo for it if it is necessary.
If you need to concat two columns with laravel query builder then i will give you example of how to concat two columns in laravel 6 application. we will concat columns in select statement and using laravel eloquent scope.
I will give you two way solution for concatenate columns in laravel 6. 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.
Solution 1:
/**
* Show the application dashboard.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$users = User::select("*", DB::raw("CONCAT(users.first_name,' ',users.last_name) as full_name"))
->get();
return view('home', compact('users'));
}
Use:
@foreach ($users as $user)
{{ $user->full_name }}
@endforeach
Solution 2:
You need to set scope in your model like as we defining on user model.
<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser 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…
Hope this code and post will helped you for implement Laravel concat two columns with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs