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

man_or_astroman's avatar

Eloquent sum relationship and group by

Hello, I have 3 tables:

financials: id, group_id, year
groups: id, name
earnings: id, amount, financial_id

I need to build a query in order to sum group earnings by year. The desired response will be like:

 {
	[2019]: [
		{
			name: group name,
			total_earnings: total earnings amount
		},
		...
	],
	[2020]: [
	....
	]
}
0 likes
0 replies

Please or to participate in this conversation.