从基数中选择时间为UNIX_TIMESTAMP(时间)laravel 5

时间:2015-04-15 12:19:48

标签: php database laravel orm laravel-5

我在基本字段中有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)

我该如何解决?

1 个答案:

答案 0 :(得分:6)

Laravel需要知道UNIX_TIMESTAMP(time) as timeu是一个RAW SQL表达式而不是列名:

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

或者:

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