php 如何使用 Laravel 4 Eloquent 连接列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22008487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:37:07  来源:igfitidea点击:

How to concatenate columns with Laravel 4 Eloquent?

phpmysqllaravellaravel-4

提问by manuthalasseril

I have a table called tenantdetailswhich contains

我有一个名为的表tenantdetails,其中包含

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

and I want to retrieve First_Nameand Last Nameas one column via the concatenation function of MySQL. So I write in my controlleras follows

并且我想通过 MySQL 的连接函数检索First_NameLast Name作为一列。所以我写在我controller的如下

$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).
 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.

在 Laravel Eloquent 中调用 MySQL 的函数时如何避免反引号。我只对 Eloquent 感兴趣(对流畅查询不感兴趣)。提前致谢。

Update

更新

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

感谢@Andreyco 帮助我。我们可以使用 Laravel 模型以更优雅的方式实现这一点,如下所示:

In our model:

在我们的model

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

and in our controller:

在我们的controller

$tenants = Tenant::orderBy('First_Name')->get();
$tenants = $tenants->lists('TenantFullName', 'Tenant_Id');

回答by Andreyco

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

回答by Michel Ayres

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

一个简单的方法是使用selectRaw. 它是由 Tailor 实施的Jan 30, 2014

Source

来源

Tenant::selectRaw('CONCAT(First_Name, " ", Last_Name) as TenantFullName, id')->orderBy('First_Name')->lists('TenantFullName', 'id'))

回答by Phailee Sharma

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

list() 方法用于从选定的结果中选择列。所以首先联系名字和姓氏,并在 select 语句中给这个列提供新的别名

 $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

然后你可以在lists()方法中选择这个别名

回答by Majbah Habib

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

您应该使用DB::raw()来连接字段

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

        )
       ->orderBy('First_Name')
       ->lists('full_name', 'Tenant_Id');