ventaquil ventaquil - 29 days ago 5
PHP Question

Select time from base as UNIX_TIMESTAMP(time) laravel 5

I have in base field with type

datetime
, it's
time
field. I want to get all elements from base and get additional
timeu
field - it's
UNIX_TIMESTAMP(time)
. I try to do this by add
->select('*','UNIXTIMESTAMP(time) AS timeu')
but Laravel gives me error. I need it to use in
->keyBy()
. I have next error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UNIX_TIMESTAMP(time)' in 'field list' (SQL: select *, `UNIX_TIMESTAMP(time)` as `timeu` from `values` where UNIX_TIMESTAMP(time) <= 1429135199 and UNIX_TIMESTAMP(time) >= 1428444000 order by `id` asc)


How can I fix it?

Answer Source

Laravel needs to know that UNIX_TIMESTAMP(time) as timeu is a RAW SQL expression and not a column name:

->select('*', \DB::raw('UNIX_TIMESTAMP(time) AS timeu'))

Or alternatively:

->selectRaw('*, UNIX_TIMESTAMP(time) AS timeu')