Group By Days Laravel Eloquent

Asked

Viewed 479 times

0

I’m having trouble grouping data together for different days. I have some reports registered and with the standard Created_at and Updated_at I’d like to show these communiqués Separated 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

ATTEMPT TO GROUP 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 WORKING ERROR inserir a descrição da imagem aqui

EDIT CONTROLLER

public function timeline(Request $request){
    $messages = Message::orderBy('created_at', 'desc')->get()
        ->groupBy(function($date) {
            return Carbon::parse($date->created_at)->format('M d'); // grouping by day
        });
    return view('messages.timeline', compact('messages'));
}

DUMP IN $messages IN VIEW

inserir a descrição da imagem aqui

I would now like to list the messages separately by month, what would be the solution?

VIEW I managed to show the day of the month but now I want to list in the messages with the same day of the month

inserir a descrição da imagem aqui

<ul class="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>
  • So, the error is the following: it expects a text parameter (or multiple columns separated by comma), what you did is not supported If you want to group by day the strategy has to be another because the way you also did is missing some things in this SQL (Builder)

  • I’m all day trying to do this and I can’t, which I should change?

  • Which SQL you need to do?

  • take a look at Edit @Virgilionovic

  • Let’s explain what you did, brought a mass of data in SQL and then in class Collection grouped the data, that is, had two effort (maybe only with SQL would already work). you want to group by month? or month and Year? I’m still in doubt what you really want, if you could put a current example of its basis and what outcome awaits?

  • I want to list all posts separately by day of the month, show once the day of the month and all posts for that day of the month. as the other Edit mine with the result of the preview, if you repair @Virgilionovic on Mar 21 day I have 6 messages and on Mar 22 day I have 1 messages

  • So it seems to me you have the result just can’t print? in your View is like? or did nothing there?

  • 1

    @Virgilionovic My view is like the print I showed you, I just managed to put the days of the month that have messages, but not able to put the messages inside, would I have to use a foreach inside another foreach? Look at Edit

Show 3 more comments

1 answer

0


You need to fetch the key and then the collection of the 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.

  • Perfect! It worked, which would be the $key?

  • $key is the key to your array needs it to print the header and inside it that has the collection you need to use in your code. @Gabrielalves

Browser other questions tagged

You are not signed in. Login or sign up in order to post.