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

joedawson's avatar

Multiple Group By?

Hello,

I currently have this query:

$performances = Performance::with('programme')
    ->orderBy('start_date', 'ASC')
    ->available()
    ->get()
    ->groupBy(function($item) {
        return $item->start_date->format('F');
    });

I'm just getting some performances and grouping them by the month.

How can I amend this to also group those within a month by the date also?

So I'd potentially have a collection with the following structure:

- Performances
    - January
        - 16th
            - [Performance]
            - [Performance]
            - [Performance]
        - 22nd
            - [Performance]
        - 29th
            - [Performance]
            - [Performance]
    - March
        - 3rd
            - [Performance]
            - [Performance]
        - 7th
            - [Performance]
    - December
        - 20th
            - [Performance]

I hope this makes sense?

0 likes
4 replies
usama.ashraf's avatar

Why not just group them by date (possibly excluding the year) ?

SaeedPrez's avatar

I believe you need to look into joins to accomplish what you're after,.. or do two queries, one for the months and one for the dates.

joedawson's avatar

@usama.ashraf that wouldn't work for me in this case.

@SaeedPrez do you know of any videos here on Laracasts that might be able to help me with writing the join as I'm not sure how to do it currently.

SaeedPrez's avatar

@JoeDawson I haven't seen any videos on joins, but you can look at the documentation for an idea and examples how to do it, or like I said, simply do two separate queries (would probably be easier and faster) and use union() to put them together, collections also have something similar.

Please or to participate in this conversation.