Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉维用拔毛法使用贝壳_Php_Laravel_Laravel 5_Concatenation_Pluck - Fatal编程技术网

Php 拉维用拔毛法使用贝壳

Php 拉维用拔毛法使用贝壳,php,laravel,laravel-5,concatenation,pluck,Php,Laravel,Laravel 5,Concatenation,Pluck,我正在使用Laravel.5.3,下面是我的问题 $ProjectManagers = Employees::where("designation" , 1) ->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id'); 这会抛出一个错误 isset中的偏移量类型非法或为空 我可以知道这是不是正确的方法吗 如果我不使用contact和like $ProjectManagers = Employees::where("

我正在使用Laravel.5.3,下面是我的问题

$ProjectManagers = Employees::where("designation" , 1)
->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id');
这会抛出一个错误

isset中的偏移量类型非法或为空

我可以知道这是不是正确的方法吗

如果我不使用contact和like

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

)

其中名字和姓氏连接在一起

尝试将雄辩的查询更改为:

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

最优雅的解决方案是创建一个

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

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}
之后,只需使用:

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

我也遇到了类似于连接查询的问题这里是我问题的解决方案

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

如果您的列可以为空,那么您应该尝试以下方法

通过
COALESCE

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

更改
->pull('first_name','id')到`->pull('name','id')@martincarlin87:SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“名称”嗯,这只是我的猜测,我想
name
必须在那里的某个地方,因为这是查询中使用的别名。我唯一能想到的是
$ProjectManagers=Employees::select([DB::raw(“CONCAT(first_name,”,last_name)AS name”))->where('designation',1)->pull('name',id')。我没有用过Pulk,所以我不确定你是否可以要求不止一列,另一种选择似乎是只使用
->(“id”,“name”)->toArray()
是的,它可以工作,谢谢,正确的一个是
$ProjectManagers=Employees::select(DB::raw(“CONCAT(first_name)”,last_name)AS name”),'id')->where('designment',1)->Pulk('name',id')太棒了!值得注意的是(尽管,可能只是我的愚蠢),要使用mutator访问所有记录,您必须编写
Employees::all()->pulk('full_name','id')
。请注意
all()
。我否定了这一点,因为我没有well子句,但是Laravel在第一次调用时查询数据库,导致它在数据库中搜索
全名
。getFooAttribute()访问器的使用非常好。这是最好的解决方案哇!给人印象深刻的这对我很有用,我觉得这很优雅!只要数据库中只有几百条记录,这就是一个很好的解决方案。对于成千上万的记录,这需要时间。性能良好的解决方案是那些不使用访问器,但在数据库级别使用concat()的解决方案。