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

martinszeltins's avatar

How to make a sub query with Eloquent

I would like to create a query like this with eloquent but is it possible?

SELECT 
    customerNumber, checkNumber, amount
FROM
    payments
WHERE
    amount = (SELECT 
            MAX(amount)
        FROM
            payments
        WHERE
             amount > 50);
0 likes
2 replies
Nakov's avatar

You can do it like this:

Payment::select('customerNumber, checkNumber, amount))
       ->havingRaw('max(amount) > ?', [50])
       ->get();

Please or to participate in this conversation.