How to select all columns with eloquent and set an alias for one of them only ?
How to select all columns with Eloquent and set an alias for one of them only ?
If I do select('MYCOLUMN as foo'), only this column is returned is the result instead of all of them as I need.
If you think the same in SQL you can do:
select *, column as alias from table
That works in PostgreSQL and MySQL but is not actually a good idea because you are getting twice the same column.
I would recommend you to type all the columns.
@devondahon select('MYCOLUMN as foo') return you 1 column because you select one column. Just extend it to select ALL columns AND aliased column
select('*', 'MYCOLUMN as foo')
@silencebringer Actually, I'm getting this error message when using select('*', 'MYCOLUMN as foo'):
[2021-09-22 10:01:06] local.ERROR: SQLSTATE[HY000]: General error: -104 Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column 9 , (SQL: select *, "MYCOLUMN" as "foo" from "my_table" where "MYCOLUMN" in (4335)) {"userId":2,"exception":"[object] (Illuminate\Database\QueryException(code: HY000): SQLSTATE[HY000]: General error: -104 Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column 9 , (SQL: select *, \"MYCOLUMN\" as \"foo\" from \"my_table\" where \"MYCOLUMN\" in (4335)) at /srv/www/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php:692)
Could it be because of Firebase 1.5 table ?
It finally works with:
select('my_table.*', 'MYCOLUMN as foo')
I have the answer boys, after three years too late. but here
use DB::RAW inside Your_model::select statement
So if your model is Example do:
Example::select(
DB::RAW('MYCOLUMN as Foo),
DB::RAW('MYCOLUMN2 as Foo2) '
)->get();
That's it.
Please or to participate in this conversation.