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

Mick79's avatar

Return items separated out by date

I am returning a bunch of items and would like them listed by date. I'm using this query

$latest = Site::query()
                ->with('upvotes')
                ->where([
                    ['is_approved', '=', 2],
                    ['is_featured', '=', 1],
                ])->orderBy('created_at', 'DESC')
                ->groupBy(DB::raw("DAY(created_at)"), 'ID')
                ->get();

I'd like to output the items on the page like this:

TODAY item 1 item 2 item 3

YESTERDAY Item 1 Item 2

2nd January Item 1 Item 2 Item 3

1st January Item 1 Item 2

etc etc

Kinda stuck.

0 likes
6 replies
tykus's avatar

A query builder groupBy is not appropriate here, you need to fetch all of the items and use the Collection groupBy:

$latest = Site::query()
    ->with('upvotes')
    ->where([
        ['is_approved', '=', 2],
        ['is_featured', '=', 1],
    ])->orderBy('created_at', 'DESC')
    ->get();

// $latest is a Collection
$latest->groupBy(function ($item) {
    return $item->created_at->toDateString();
});
nolros's avatar

You could try something like this, but unsure if you are saving anything.

        $latest = Site::query()
            ->with('upvotes')
            ->where([
                ['is_approved', '=', 2],
                ['is_featured', '=', 1],
            ])->orderBy('created_at', 'DESC')
            ->get()
            ->map(function ($sites) {
                $sites->groupBy(function ($s, $k) {
                    return $s->created_at->toDateString();
                });
                return $sites;
            });
Mick79's avatar

@TYKUS - Hi thanks for this. My code now looks like this:

Controller

$latest = Site::query()
                ->with('upvotes')
                ->where([
                    ['is_approved', '=', 2],
                    ['is_featured', '=', 1],
                ])->orderBy('created_at', 'DESC')
                ->groupBy(DB::raw("DAY(created_at)"), 'ID')
                ->get();

            // New section
            $latest->groupBy(function ($item) {
                return $item->created_at->toDateString();
            });


            return view('home', compact( 'user', 'featured', 'latest', 'voted', 'y'));

Blade

@foreach ($latest as $product)

//output the product details

@endforeach

But how do I get each loop to start with the date? Bear in mind I just want each loop to start with the date and not each product/item.

Thanks so much for taking the time to reply.

Mick79's avatar

@NOLROS - Hi thanks for replying, when I use this code I get the same output as I'm getting at the moment. I fear I'm missing something. Do I need to change the foreach in the blade to start the loop with the date?

Thanks

Mick79's avatar

Hi Richard that didn't change anything. It didn't break anything, but it didn't change anything either. I'm just getting all the products output as they were.

I feel like I'm missing something. This shouldn't be hard.

Please or to participate in this conversation.