php - Laravel - 找不到列:'full_name'
问题描述
在我的laravel-8中,我有一个模型尝试连接:
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();
我如何实现这一目标?
谢谢
解决方案
你可以通过这样的别名来实现这一点
Employee::select(\DB::raw("CONCAT(`first_name`,' ',`last_name`) AS `full_name`"))->get();
推荐阅读
- css - 带有 flexbox 和动态内容的 CSS 网格
- java - Java 11 中的 WebSocket 实现
- python - 如何在Python中达到到期日期时自动删除部分股票?(先进先出方法内)
- javascript - 检查一个数组是否包含另一个数组的任何元素,在 JavaScript 中有一些 null
- javascript - 可拖动的 div 并单击到元素后面
- python - 根据熊猫的情况计算天差
- c# - 将 JSON 值中的“k1:v1,k2:v2,..”还原为键值对
- c++ - 当作为 const 引用传递给基类时,为什么 shared_ptr 计数器会增加?
- c# - 如何使用 RestSharp 的 AddFile() 方法发送 httpContext.Request.Files[0]?
- python - 如何通过聚合另一列满足条件的值来创建新列?