Farshid Boroomand
Farshid Boroomand

Reputation: 13

How can i change this query to 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
)

Upvotes: 0

Views: 928

Answers (1)

Victor Anuebunwa
Victor Anuebunwa

Reputation: 2893

You can use selectRaw() and DB::raw() to handle complex SQL statements

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"));

Upvotes: 1

Related Questions