How to concat two columns in laravel using eloquent model
How to concat two columns in laravel using eloquent model
Lets take an example of Customers which has first_name, last_name, email, etc.
Now, I want to concatinate "first_name and last_name" together.
Lets concate columns in laravel eloquent model as follows:
class Customer extends Model
{
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
}
Lets get the full name of customer:
Example 1: In the Controller
$customer = Customer::find(1);
return $customer->FullName;
return $customer->full_name;
Example 2: In the Controller
$customers = Customer::all();
foreach ($customers as $data) {
echo $data->FullName;
echo $data->full_name;
}
Example 3: In blade file
@foreach ($customers as $data)
<h6>{{ $data->FullName }}</h6>
<h6>{{ $data->full_name }}</h6>
@endforeach