I have a table called tenantdetails
which contains
Tenant_Id | First_Name | Last_Name | ........
and I want to retrieve Firs
Tenant::select('Tenant_Id', DB::raw('CONCAT(First_Name, " ", Last_Name) AS full_name'))
->orderBy('First_Name')
->lists('full_name', 'Tenant_Id');
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
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'))
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');