Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4896

Complicated Laravel Query

I want to convert the following SQL query to Laravel Eloquent or Query

SELECT
    SUM(T1.total) AS Total,
    SUM(T1.received) AS Received,
    T1.EventName,
    T1.CurrencyType
FROM
    (
        SELECT
            event_invoice.Id,
            event_invoice.Amount AS total,
            SUM(payments.recieved_amount + payments.adjust_amount) AS received,
            event_invoice.EventName,
            event_invoice.CurrencyType
        FROM
            event_invoice
        LEFT JOIN payments ON event_invoice.Id = payments.invoice_id
        GROUP BY
            event_invoice.Id
        ORDER BY
            event_invoice.Id
    ) T1
GROUP BY
    T1.EventName,
    T1.CurrencyType

I wanted to convert into Laravel any Ideas ??

Upvotes: 0

Views: 53

Answers (1)

GeoChatz
GeoChatz

Reputation: 668

Try something like this:

$results = \DB::select( \DB::raw("
    SELECT
        SUM(T1.total) AS Total,
        SUM(T1.received) AS Received,
        T1.EventName,
        T1.CurrencyType
    FROM
        (
            SELECT
                event_invoice.Id,
                event_invoice.Amount AS total,
                SUM(payments.recieved_amount + payments.adjust_amount) AS received,
                event_invoice.EventName,
                event_invoice.CurrencyType
            FROM
                event_invoice
            LEFT JOIN payments ON event_invoice.Id = payments.invoice_id
            GROUP BY
                event_invoice.Id
            ORDER BY
                event_invoice.Id
        ) T1
    GROUP BY
        T1.EventName,
        T1.CurrencyType
") );

Upvotes: 1

Related Questions