Support the ongoing development of Laravel.io →
posted 11 years ago
Blade

I'm trying to figure out the best way to present data in a grouped fashion.

I have a load of courses that are displayed on the view within a @foreach( ) statement, the results are ordered by date. I have results from now up until this December for each month has quite a few records. What I'd like to do is group the month results under a header for that month, eg.

@foreach($Records as $key => $Record)
  <?php $current = Date('mY', $Record->Date); ?>
  @if( $key == 0 )
    <?php $previous = ''; ?>
  @endif
  @if( $current != $previous )
    <p>{{ Date('mY', $Record->Date) }}</p>
  @endif
  <p>Display this record for {{ $Record->id }}</p>
  <?php $current = Date('mY', $Record->Date); ?>
@endforeach

This works, but I'm not overly joyed about having to embed php tags in the blade file and was just wondering if there was a better way to approach this. I've had a go at extending blade as per the documentation with not much success.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

thevincent thevincent Joined 11 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.