首页 > 解决方案 > Laravel - 找不到列:'full_name'

问题描述

在我的中,我有一个模型尝试连接:

protected $appends = 'full_name';

public function getFullNameIdAttribute()
{
    return ucfirst($this->last_name) . ', ' . ucfirst($this->first_name). ' ' . ucfirst($this->other_name);
}

我想连接name模型中的员工。

控制器:

public function employeeParameters()
{
    try {
        $employees = Employee::select('full_name', 'id')->get();
        return $this->success('Vehicle Detail.', [
            'employees' => $employees,
        ]);
    } catch(\Throwable $e) {
        Log::error($e);

        return $this->error($e->getMessage(), $e->getCode());
    }
}

但我得到了这个错误:

local.ERROR:PDOException:SQLSTATE [42S22]:未找到列:1054 未知列 'full_name'

并指向这一行:

$employees = Employee::select('full_name', 'id')->get();

我如何实现这一目标?

谢谢

标签: phplaravellaravel-8

解决方案


你可以通过这样的别名来实现这一点

Employee::select(\DB::raw("CONCAT(`first_name`,' ',`last_name`) AS `full_name`"))->get();

推荐阅读