sql - Laravel Display a record details while grouped by year or month -


i moved laravel framework , starting migrate legacy sites , have hit problem sql or blade - dunno which.

i have display load of rows 'sports classes' grouped year , month. each needs show attendance etc.

i unsure way proceed. able display rows , sort date - easy squeezy able groupby year , month - fiddly sorted it. these displayed in accordian. click month - individual rows drop down - idea

i can number of rows per month/year unable figure out how display rows.

the groupby this:

$linkclasses = db::table('classes_lists') ->select('id, class, teacher, size') ->select(db::raw('year(date) year, month(date) month, monthname(date) month_name, count(*) post_count'))     ->groupby('year')     ->groupby('month')     ->orderby('year', 'desc')     ->orderby('month', 'desc')     ->orderby('id', 'desc') 

if code provided within controller, can append ->get() after last ->orderby(). return collection. can whatever want collection (http://laravel.com/api/master/illuminate/support/collection.html), including conversion array using ->toarray(), think best utilize eloquent orm if possible.

anyway, once have in format want, pass view so:

return view('your.view', compact('linkclasses')); 

then, inside your.view blade template, can access using following:

@foreach ($linkclasses $currentrow)   <tr>     <td>{{ $currentrow['id'] }}</td>     <td>{{ $currentrow['class'] }}</td>     <td> ... </td>   </tr> @endforeach 

best guess can offer without seeing blade template better idea of you're doing. hope helps!

update based on op feedback:

since receiving single record, seems though issue lies in query. suggest simplify query fetch records , sorting within array. in controller:

$allclasses = db::table('classes_lists')->all(); foreach ($allclasses $currentclass) {     $yearmonth = date('y-m', $currentclass['date']);     $classesbyyearmonth[$yearmonth][] = $currentclass; } ksort($classesbyyearmonth);  /* have array of classes sorted year-month this: // $classesbyyearmonth[2014-01] = array( //     [0] => array(1, 'class name', 'teacher name', 23), //     [1] => array(2, 'another class', 'different teacher', 25), //     ... // ); // // $classesbyyearmonth[2014-02] = ... */  return view('your.view', compact('classesbyyearmonth')); 

then, inside blade template:

@foreach ($classesbyyearmonth $yearmonth => $classlistarray)     found {{ sizeof($classlistarray) }} classes {{ $yearmonth }}     @foreach ($classlistarray $currentclass)         <div>             <div>id: {{ $currentclass['id'] }}</div>             <div>class: {{ $currentclass['class'] }}</div>             <div>teacher: {{ $currentclass['teacher'] }}</div>             <div>size: {{ $currentclass['size'] }}</div>         </div>     @endforeach @endforeach 

i leave fix formatting make accordion work. on right path.