Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid column name ' ' on concatenate columns with Laravel 4 Eloquent?

So I want to prepare a list to place into a combobox with laravel and I wanted to use concatenate to join the value of 3 columns and I can achieve that with this

public function deviList() {
        return Device::select(DB::raw('CONCAT(DESC, OS, OS_V) AS FullDesc'), 'DEVI_ID')                        
                        ->where('STATUS', 2)
                        ->orderBy('DESC')
                        ->lists('FullDesc', 'DEVI_ID');
    }

However it'd be better to have a space or slash separating the values of the columns so I did it like this, same as some people recomended in some other places:

public function deviList() {
        return Device::select(DB::raw('CONCAT(DESC," ",OS," ",OS_V) AS FullDesc'), 'DEVI_ID')                        
                        ->where('STATUS', 2)
                        ->orderBy('DESC')
                        ->lists('FullDesc', 'DEVI_ID');
    }

However I get an error saying SQLSTATE[42S22]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name ' ' (So I'm guessing unlike the examples given elsewhere, those separations get read as if they were columns?) So how can I concatenate with some sort of separation?

like image 737
Micael Florêncio Avatar asked Jan 23 '15 15:01

Micael Florêncio


1 Answers

If your SQL Server has QUOTED_IDENTIFIER turned on, then the double quotes will be used to identify objects, not string literals. In this case, " " would not be a string representing a space, it would be an object (column) represented by a space, which doesn't exist.

Try switching the quotes in your SQL statement:

public function deviList() {
    return Device::select(DB::raw("CONCAT(DESC,' ',OS,' ',OS_V) AS FullDesc"), 'DEVI_ID')                        
        ->where('STATUS', 2)
        ->orderBy('DESC')
        ->lists('FullDesc', 'DEVI_ID');
}

QUOTED_IDENTIFIER docs here.

like image 98
patricus Avatar answered Oct 01 '22 18:10

patricus