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

dixitchopra's avatar

Find monthly Sale in Eloquent

How can I write the following in Eloquent or is there any other way to find monthly sales group by Date? Can I also use limit here like 20 records in results?

SELECT DATE(created_at) AS date, SUM(final_amount) AS total_sales
FROM bills
GROUP BY date
0 likes
3 replies
JarekTkaczyk's avatar

@dixitchopra I don't think you want Eloquent for things like reports. Apart from that, it is pretty basic query, so what do you have problem with here?

dixitchopra's avatar

I thought alias and group by can be written using Eloquent and without DB:Raw. I have written the code now with DB:Raw.

JarekTkaczyk's avatar

@dixitchopra You're talking about Query\Builder. Eloquent is the ORM part, that uses query builder.

Anyway, you can alias select('column as alias') and group by without raw, but sql functions cannot be called without it:

DB::table('bills')->selectRaw('date(created_at) as date, sum(final_amount)  as total_sales')
    ->groupBy('date')
    ->get();
1 like

Please or to participate in this conversation.