Tatata
Tatata

Reputation: 31

Laravel group by date in foreach not working

I have the following code:

$getDate = $messages->groupBy(function($date) { 
    return Helper::ru_date('%d %b', strtotime($date->created_at->toDateString())); 
});

This give me messages group by date. I get an array:

14 february => messages items
15 february => messages items

When I want do foreach and show date I get object of message. Why? I want see 14 february or 15.

My foreach:

@foreach($getDate as $date)
   {{ $date }} //I get collection of message, but not date
   @foreach($date as $message)
        //but this I need show messages of this date
   @endforeach
@endforeach

Upvotes: 1

Views: 1221

Answers (2)

SRK
SRK

Reputation: 3496

Try this.

@foreach($getDate as $date => $messages)
   {{ $date }} 
   @foreach($messages as $message)
        {{ $message }}
   @endforeach
@endforeach

Upvotes: 1

Nico Haase
Nico Haase

Reputation: 12105

If getDate is an array with the date in the keys and the messages in the values, you should adjust your loop head to:

@foreach($getDate as $date => $messages)

Upvotes: 0

Related Questions