How to concatenate columns with Laravel 4 Eloquent?

Problem :

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.


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');

Solution :

Tenant::select('Tenant_Id', DB::raw('CONCAT(First_Name, " ", Last_Name) AS full_name'))
    ->lists('full_name', 'Tenant_Id');

Mysql Tutorials

Mysql Howto..

How to delete mySQL records that are empty? [closed]

how to remove duplicated columns and group column

How to use data from a different table to obtain the appropriate records in MySQL

How to run multiple file .sql with mysql?

How do I prevent MySQL from auto-incrementing the Primary Key while using ON DUPLICATE KEY UPDATE when the duplicate is a different unique column?

How to store 'query-able' encrypted data in MySQL?

How to subtract 30 days from the current datetime in mysql?

How to write a query for creating an stored proceedure in mysql

how to import mysql database

How to get Ruby on Rails (Rails v4, Ruby v1.9.3) to work with MySQL

How do I search “many LIKE” in the database (mysql)?

How to search in mysql so that accented character is same as non-accented?

MySQL, How to add a points system? (stacking variables in a row.)

How best to handle Flyway with embedded DB for integration tests?

how to create few tables in a single query. MySQL

mysql - how to include zeros in the count in only one single query

How do I skip an item in while loop if variable empty?

How do I make this select in Single query?

How can I group elements in HTML while printing query results?

SPHINX : How to set the matchMode on the mysql client

How to create a CHECK constraint in MySQL that requires negative values?

How to generate random item with different frequency from mysql

How to put value from MySQL cell into a variable? [closed]

How to configure mySQL on Eclipse EE (Java)

How to create script with Insert statement for the records selected in MySql?

How to tell which columns have been set to CASCADE in mysql

Mysql: How to retrieve data on weekly basis

How to connect mySql database using sequel pro? [closed]

How to remove records in mysql database? [closed]

how to work on mysql date fomat?