php - Complicated Laravel Query

648

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 ??

418

Answer

Solution:

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

People are also looking for solutions to the problem: Insert data into mysql using OOP PHP and PDO

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.