Laravel使用concat和pluck方法

时间:2016-10-19 12:56:43

标签: laravel concat laravel-5.3 pluck

我使用的是Laravel.5.3,以下是我的查询

$ProjectManagers = Employees::where("designation" , 1)
->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id');

会抛出错误

  

isset中的非法偏移类型或空

我知道这是否是正确的方法?

如果我不使用联系方式并使用

$ProjectManagers = Employees::where("designation" , 1)->pluck('first_name','id');

这是正确的,并给我结果

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [8] => Punit
        )

)

预期结果:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [8] => Punit Gajjar
        )

)

其中连接名字和姓氏。

4 个答案:

答案 0 :(得分:35)

Try changing the eloquent query to:

$ProjectManagers = Employees::select(
            DB::raw("CONCAT(first_name,' ',last_name) AS name"),'id')
            ->where('designation', 1)
            ->pluck('name', 'id');

答案 1 :(得分:26)

最优雅的解决方案是创建accessor

打开员工类(模型)并添加访问者功能:

KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

#WSGIDaemonProcess application processes=2 threads=12 python-path=/home/device_name/webapps/application:/home/device_name/webapps/application/project_name:/home/device_name/webapps/application/lib/python2.7
#WSGIProcessGroup application
#WSGIRestrictEmbedded On
#WSGILazyInitialization On
#WSGIScriptAlias / /home/device_name/webapps/application/project_name/project_name/wsgi.py

# Virtual hosts setup
NameVirtualHost *
<VirtualHost *>
ServerName application.com

WSGIDaemonProcess application processes=5 python-path=/home/device_name/webapps/application:/home/device_name/webapps/application/project_name:/home/device_name/webapps/application/lib/python2.7 threads=1
WSGIProcessGroup application
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/device_name/webapps/application/project_name/project_name/wsgi.py
</VirtualHost>

<VirtualHost *>
ServerName dreamhomeconsultancy.in

WSGIDaemonProcess APPLICATION_NAME_www processes=5 python-path=/home/device_name/webapps/application:/home/device_name/webapps/application/civilengg:/home/device_name/webapps/application/lib/python2.7 threads=1
WSGIProcessGroup civilengg
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/device_name/webapps/application/civilengg/civilengg/wsgi.py
</VirtualHost>

之后,只需使用:

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

答案 2 :(得分:2)

如果您的列可为空,则应尝试此操作

通过NULL用空字符串转换COALESCE

$ProjectManagers = Employees::select(
            DB::raw("CONCAT(COALESCE(`first_name`,''),' ',COALESCE(`last_name`,'')) AS name"),'id')
            ->where('designation', 1)
            ->pluck('name', 'id')
            ->toArray();

答案 3 :(得分:1)

我也遇到过像这样的问题,加入查询就是我的问题的解决方案

$studentDegree = Student::leftJoin('degree','degree.student_id','=','student.id')
->select(
      DB::raw("CONCAT(student.name,'-',degree.name) AS name_degree"),
      'student.id'
)->lists('name_degree','id');