Group By Days Laravel Eloquent

0

I am not able to group data for different days. I have some communications registered and with the default Created_at and Updated_at I would like to show these separate releases day by day .. VIEW

@foreach($messages as $message)
        <li class="time-label">
        <span class="bg-red">
            {{  $message->updated_at }}
        </span>
        </li>
        <li>
            <i class="fa fa-envelope bg-blue"></i>
            <div class="timeline-item">
                <span class="time"><i class="fa fa-clock-o"></i> </span>

                <h3 class="timeline-header"><a href="#"></a> ...</h3>

                <div class="timeline-body">
                    {{ $message->message }}
                </div>
                <div class="timeline-footer">
                    <a class="btn btn-primary btn-xs">...</a>
                </div>
            </div>
        </li>
    @endforeach

TRY BY DAY CONTROLLER

public function timeline(Request $request){
    $messages = Message::where('type_send', 1)->groupBy(function($date){
        return Carbon::parse($date->created_at)->format('D');
    });

    return view('messages.timeline', compact('messages'));
}

NOT RUNNING ERROR

EDITCONTROLLER

publicfunctiontimeline(Request$request){$messages=Message::orderBy('created_at','desc')->get()->groupBy(function($date){returnCarbon::parse($date->created_at)->format('Md');//groupingbyday});returnview('messages.timeline',compact('messages'));}

DUMPIN$NANAVIEW

Iwouldliketonowlisttheseparatemessagespermonth,whatwouldbethesolution?

VIEWIwasabletoshowthedayofthemonth,butnowIwanttolistthemessagesonthesamedayofthemonth

<ulclass="timeline">
    @foreach($messages as $message)
        <li class="time-label">
        <span class="bg-red">

            {{  $message->first()->updated_at }}
        </span>
        </li>
                <li>
                    <i class="fa fa-envelope bg-blue"></i>
                    <div class="timeline-item">
                        <span class="time"><i class="fa fa-clock-o"></i> </span>
                        <h3 class="timeline-header"><a href="#"></a></h3>
                        <div class="timeline-body">
                        </div>
                        <div class="timeline-footer">
                            <a class="btn btn-primary btn-xs">...</a>
                        </div>
                    </div>
                </li>
    @endforeach

</ul>
    
asked by anonymous 21.03.2018 / 22:25

1 answer

0

You need to fetch the key and then the collection of the respective key:

@foreach($messages as $key => $message) 
    <li class="time-label">
    <span class="bg-red">
        {{  $key }}
    </span>
    </li>
    @foreach($message as $item)
        <li>
            <i class="fa fa-envelope bg-blue"></i>
            <div class="timeline-item">
                <span class="time"><i class="fa fa-clock-o"></i> </span>

                <h3 class="timeline-header"><a href="#"></a> ...</h3>

                <div class="timeline-body">
                    {{ $item->message }}
                </div>
                <div class="timeline-footer">
                    <a class="btn btn-primary btn-xs">...</a>
                </div>
            </div>
        </li>
    @endforeach
@endforeach

This is through your comments.

    
22.03.2018 / 14:25