Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate columns with Laravel 4 Eloquent?

I have a table called tenantdetails which contains

Tenant_Id | First_Name | Last_Name | ........

and I want to retrieve First_Name and Last Name as one column via the concatenation function of MySQL. So I write in my controller as follows

$tenants = Tenant::orderBy('First_Name')->lists('CONCAT(`First_Name`," ",`Last_Name`)','Tenant_Id');

But results the following error:

 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
 in your SQL syntax; check the manual that corresponds to your MySQL server
 version for the right syntax to use near '`," ",`First_Name`)`, 
 `Id` from `tenantdetails` order by `F' at line 1 

 (SQL: select `CONCAT(`First_Name`," ",`Last_Name`)`, `Id` 
 from `tenantdetails` order by `First_Name` asc).

How can we avoid the backticks while calling a function of MySQL in Laravel Eloquent. I am interested only in Eloquent (not in fluent query). Thanks in advance.

Update

Thanks to @Andreyco for helping me. We can achieve this in a more elegant way using Laravel models, as below:

In our model:

public function getTenantFullNameAttribute()
{
    return $this->attributes['First_Name'] .' '. $this->attributes['Last_Name'];
}

and in our controller:

$tenants = Tenant::orderBy('First_Name')->get();
$tenants = $tenants->lists('TenantFullName', 'Tenant_Id');
like image 655
manuthalasseril Avatar asked Feb 25 '14 08:02

manuthalasseril


People also ask

What is difference between eloquent and query builder?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.

Is Laravel eloquent slow?

Using eloquent is great because its have many cool features but when it come to the speed, it slightly slower than query builder because of ORM.

What is advantage of eloquent in Laravel?

CRUD with Eloquent. CRUD operations under the Eloquent object-relational mapper (ORM) make it easier for Laravel developers to work with multiple databases. It performs create, retrieve, update, and delete (CRUD) operations, as well as maps object models to database tables.


4 Answers

Tenant::select('Tenant_Id', DB::raw('CONCAT(First_Name, " ", Last_Name) AS full_name'))
    ->orderBy('First_Name')
    ->lists('full_name', 'Tenant_Id');
like image 200
Andreyco Avatar answered Oct 21 '22 06:10

Andreyco


An easy way is to use selectRaw. It was implemented by Tailor in Jan 30, 2014

Source

Tenant::selectRaw('CONCAT(First_Name, " ", Last_Name) as TenantFullName, id')->orderBy('First_Name')->lists('TenantFullName', 'id'))
like image 26
Michel Ayres Avatar answered Oct 21 '22 04:10

Michel Ayres


lists() method used to select column from selected result. So first contact first name and last name and give this column with new alias name in select statement

 $tenants = Tenant::orderBy('First_Name')->select(DB::row('CONCAT(`First_Name`," ",`Last_Name`) as name'),'Tenant_Id')->lists('name', 'id');

then you can select this alias in lists() method

like image 3
Phailee Sharma Avatar answered Oct 21 '22 04:10

Phailee Sharma


You should use the DB::raw() to concat those of field

Tenant::select(
          'Tenant_Id',
          DB::raw('CONCAT(First_Name,"-",Last_Name) as full_name')

        )
       ->orderBy('First_Name')
       ->lists('full_name', 'Tenant_Id');
like image 2
Majbah Habib Avatar answered Oct 21 '22 05:10

Majbah Habib