1

我想选择两列并将其显示为我的控制器中的一列。这是我目前拥有的代码:

public function assignment()
{
    $title = "View Parent Assignment";
    $vpc   = DB::table('dbo_guardianchild')
                ->join('dbo_students', 'dbo_guardianchild.StudentID', '=' , 'dbo_students.StudentID')
                ->join('dbo_guardianinformation' , 'dbo_guardianchild.GuardianInformationID' , '=' , 'dbo_guardianinformation.GuardianInformationID')
                ->select('dbo_students.StudentID' , 'dbo_students.FirstName AS sFname' , 'dbo_students.LastName AS sLname')
                ->get();
}

关于如何将dbo_students.FirstNameanddbo_students.LastName合并为一列的任何想法?

4

1 回答 1

5

你应该试试 DB::raw。尝试将选择中的代码替换为

    ->select(DB::raw('dbo_students.StudentID' ,'CONCAT(dbo_students.FirstName, " ", dbo_students.LastName) AS full_name'))

你的最终代码将是

public function assignment()
{
$title = "View Parent Assignment";
$vpc   = DB::table('dbo_guardianchild')
            ->join('dbo_students', 'dbo_guardianchild.StudentID', '=' , 'dbo_students.StudentID')
            ->join('dbo_guardianinformation' , 'dbo_guardianchild.GuardianInformationID' , '=' , 'dbo_guardianinformation.GuardianInformationID')
            ->select(DB::raw('dbo_students.StudentID' ,'CONCAT(dbo_students.FirstName, " ", dbo_students.LastName) AS full_name'))
            ->get();
}
于 2015-10-09T05:59:05.987 回答