Ibe
305
2
Laravel

How to sort a groupBy?

Posted 5 months ago by Ibe

Hi there, I am having a problem to sort a groupBy.. So this code works perfectly, but the problem is my years are displaying in a random order.. So what I would love is to see the most recent year on top and the others following. Can someone help me pls?

Controller

  public function getCommemorative(){
    $commemoratives = Commemorative::orderBy('month', 'desc')->paginate(15)->groupBy('year');

    return view('pages/commemorative')->withCommemoratives($commemoratives);
  }

Blade

@foreach ($commemoratives as $year => $commemoratives)

    <h1>{{ $year }}</h1>

    @foreach ($commemoratives->where('jointly_id', '') as $commemorative)

        <div class="">
          <img src="/images/commemorative-coins/{{$commemorative->image}}" class="" alt="">
        </div>

    @endforeach

@endforeach

Thanks!!

Please sign in or create an account to participate in this conversation.