Search code examples
phplaravellaravel-5laravel-notification

How do you change the sorting of Laravel Notifications


I am printing the notifications for a Model in Laravel 5:

@foreach ($booking->notifications as $notification)                     
        <div class="message">
            <div class="myMessage">
              <p>{{ $notification->data[0])}}</p>
            </div>
        </div>                      
@endforeach

These notifications are being printed in descending order, is there a way I can change this so they print in ascending order?

The Laravel documentation states:

By default, notifications will be sorted by the created_at timestamp

It's worth mentioning that there are multiple Notification classes that have been used with this model so I'm not sure if that affects the sorting behavior?


Solution

  • @Peyman Seraj led me in the right direction here.

    With Laravel collections you can use collection methods, so I used the sortBy() method on my collection:

    @foreach ($booking->notifications->sortBy('created_at') as $notification)
    

    This now prints the data in ascending order.