Laravel collection group by

Viewed 45249

I have a search function to search for expenses by a certain date, month, string, whatever. It returns a collection:

enter image description here

So far so good. I can display all returned Expenses. Now, what I want to do is, display also a total expense count and total amount, grouped by the description_id. If I simply do $result->groupBy('description_id), I just get a set of collections, from which I can display the total count and total amount, but not the name of the description. Example of what I want:

Grouped Results (By Expense):

Description Name: <-- Issue displaying this
Total Expenses: 3
Total Amount: 53444

The description name can only be obtained via the relation between Expense and Description. An Expense belongsTo a description.

Is it at all possible to achieve what I want, when working with my collection, or should I perform a seperate query for those results? (undesired)

1 Answers
Related