如何将此查询更改为Laravel Eloquent?

时间:2017-07-26 16:10:20

标签: php laravel eloquent

SELECT to_date(
           (
             SELECT MAX(TO_CHAR(from_tz(cast(CREATE_UTC_DATETIME AS TIMESTAMP), 'UTC') AT TIME ZONE 'Asia/Tehran', 'YYYY-MM-DD')) GROUP_DATE
             FROM REPORT_EVENTS
           ), 'yyyy-mm-dd') - (ROWNUM * 7) group_date
FROM (SELECT 1 counter
      FROM dual
           CONNECT BY LEVEL <= 52
)

1 个答案:

答案 0 :(得分:1)

您可以使用selectRaw()DB::raw()来处理复杂的SQL语句

YourModel::selectRaw("to_date(("
            . "SELECT MAX(TO_CHAR(from_tz(cast(CREATE_UTC_DATETIME AS TIMESTAMP), 'UTC') AT TIME ZONE 'Asia/Tehran', 'YYYY-MM-DD')) GROUP_DATE "
            . "FROM REPORT_EVENTS), 'yyyy-mm-dd') - (ROWNUM * 7) group_date")
    ->from(DB::raw("SELECT 1 counter FROM dual CONNECT BY LEVEL <= 52"));