将两列显示为一列

时间:2015-10-09 05:27:18

标签: laravel laravel-4 laravel-4.2

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

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.FirstNamedbo_students.LastName合并为一列的任何想法?

1 个答案:

答案 0 :(得分:4)

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

    ->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();
}