我在基本字段中有datetime
类型,它是time
字段。我希望从基础获取所有元素并获得额外的timeu
字段 - 它是UNIX_TIMESTAMP(time)
。我试着通过添加->select('*','UNIXTIMESTAMP(time) AS timeu')
来做到这一点,但是Laravel给了我错误。我需要在->keyBy()
中使用它。我有下一个错误:
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)
我该如何解决?
答案 0 :(得分:6)
Laravel需要知道UNIX_TIMESTAMP(time) as timeu
是一个RAW SQL表达式而不是列名:
->select('*', \DB::raw('UNIX_TIMESTAMP(time) AS timeu'))
或者:
->selectRaw('*, UNIX_TIMESTAMP(time) AS timeu')