简体   繁体   中英

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?

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM