Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nurularifin's avatar

I need to translate this MySQL code to Laravel Eloquent

Can anyone help me, please? I wanted to get the SUM of the total amount, so I tried using full MySQL code, and it worked. But I don't know how to implement it in Laravel Eloquent. Here is my code:

SELECT SUM(total_amount) as total FROM payments WHERE status  = 'confirm' AND id = ANY (SELECT payment_id FROM orders WHERE instructor_id = '6' GROUP BY payment_id);

Any help is much appreciated.

0 likes
2 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
Payment::where('status', 'confirm')
    ->whereIn(
        'id',
        Order::select('payment_id')->where('instructor_id', 6)->groupBy('payment_id')
    )->sum('total_amount');

Please or to participate in this conversation.