Search code examples
laravellaravel-4laravel-4.2

Display two columns as one


I wanted to select two columns and display it as one in my controller. Here is the code I currently have:

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

Any ideas on how I can combine the dbo_students.FirstName and dbo_students.LastName into one column?


Solution

  • You should try DB::raw. Try replacing the code in select as

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

    And your final code will be

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