我试图将名字,中间名,姓氏与RAW结合使用,但失败了。 我的方式错了吗?谢谢
$student = DB::table('student')
->select(DB::raw('RTRIM(LTRIM(CONCAT(
COALESCE(FirstName + ''),
COALESCE(MiddleName + ''),
COALESCE(Lastname, ''))))
AS Name'))
->get();
$student = DB::table('student')
->select(
DB::raw("TRIM(CONCAT(FirstName,' ',MiddleName,' ',LastName)) AS Name")
)->get();
TRIM 函数 - 从字符串中删除前导空格和尾随空格 查看示例以及如何使用它
CONCAT函数 - 使用逗号将多个字符串加在一起:查看示例以及如何使用它
希望能帮助你:)
为什么不使用Laravel模型的方式来实现这一目标?
class Student extends Model {
protected $appends = 'full_name';
public function getFullNameAttribute() {
return $this->FirstName . ' ' . $this->MiddleName . ' ' . $this->LastName;
}
}
然后,Student::get()
将具有每个学生full_name
属性。
+
符号应为逗号:
$student = DB::table('student')
->selectRaw("TRIM(CONCAT(COALESCE(FirstName, ''), COALESCE(MiddleName, ''), COALESCE(Lastname, ''))) AS Name")
->get();
试试这个:
$student = DB::table('student')
->select(DB::raw('CONCAT_WS(" ", `FirstName`, `MiddleName`, `Lastname`) as Name'))
->get();
试试这个:
$student = Student::select(DB::raw('CONCAT_WS(" ", `FirstName`, `MiddleName`, `Lastname`) as Name'))
->get();