ventaquil ventaquil - 5 months ago 82
PHP Question

Select time from base as UNIX_TIMESTAMP(time) laravel 5

I have in base field with type

, it's
field. I want to get all elements from base and get additional
field - it's
. I try to do this by add
->select('*','UNIXTIMESTAMP(time) AS timeu')
but Laravel gives me error. I need it to use in
. 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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download