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

Ibe's avatar
Level 1

How to show items per day (created_at)

Hi there, I don't know how I can sort all the rows that belongs to a date in one section.

I would like to achieve this: http://prntscr.com/j6f73w

So I would love to show the items sorted by date (created_at), does anyone now how please?

ActoneController:

public function index()
    {
        $actones = Actone::orderBy('created_at', 'desc')->paginate(50);
    
        return view('admin.activities.actone.index')->withActones($actones);
    }

index.blade.php

     @foreach ($actones as $actone)
     <div class="card">
       <div class="card-content">

                 <h1>{{$actone->id}}</h1>
                 <h1>{{$actone->category}}</h1>
                 <h1>{{$actone->time}}</h1>
                 <h1>{{$actone->name}}</h1>

                 <h1>{{date('d/m/Y - H:i', strtotime($actone->created_at))}}</td>

       </div>
     </div> <!-- end of .card -->
     @endforeach 

Thanks!!

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

You can use the groupBy collection method to group all records by date:

public function index()
{
        $actones = Actone::latest()->paginate(50)
        ->groupBy(function ($acetone) {
            return $acetone->created_at->toDateString();
        });
    
        return view('admin.activities.actone.index')->withActones($actones);
}

... and then you will have nested collections to iterate over (I am showing a div with a group class for containing the cards):

@foreach($acetones as $date => $group)
    <div class="group">
    {{ $date }}

    @foreach ($group as $acetone)
        <div class="card">
            <div class="card-content">

                <h1>{{$actone->id}}</h1>
                <h1>{{$actone->category}}</h1>
                <h1>{{$actone->time}}</h1>
                <h1>{{$actone->name}}</h1>

                <h1>{{date('d/m/Y - H:i', strtotime($actone->created_at))}}</td>
    
            </div>
        </div> <!-- end of .card -->
    @endforeach
    </div>
@endforeach 
1 like
rin4ik's avatar

@tykus nice . this should do the trick. you have little typo here

@foreach($acetones as $date => $group)
1 like
Ibe's avatar
Level 1

@tykus OMG U are amazing sir!! I have been searching so long for this... Really thanks!

Please or to participate in this conversation.