Laravel Display a record details while grouped by year or month

女生的网名这么多〃 提交于 2019-12-24 13:12:31

问题


I just moved to the laravel framework and am starting to migrate some legacy sites and I have hit a problem with SQL or blade - dunno which.

I have to display a load of rows 'sports classes' which are grouped by year and then month. each needs to show attendance etc.

I am unsure which way to proceed. I am able to display all rows and sort by date - easy squeezy I am able to groupBy year AND month - fiddly but sorted it. These are all displayed in an accordian. Click the month - the individual rows drop down - you get the idea

I can get a number of rows per month/year What I am unable to figure out is how to actually display the rows.

The groupBy is this:

$LinkClasses = DB::table('classes_lists')
->select('id, class, teacher, size')
->select(DB::raw('YEAR(date) AS year, MONTH(date) AS month, MONTHNAME(date) AS month_name, COUNT(*) post_count'))
    ->groupBy('year')
    ->groupBy('month')
    ->orderBy('year', 'desc')
    ->orderBy('month', 'desc')
    ->orderBy('id', 'desc')

回答1:


If the code you provided is within your controller, then you can append ->get() after your last ->orderBy(). This will return a Collection. You can then do whatever you want with the Collection (http://laravel.com/api/master/Illuminate/Support/Collection.html), including conversion to an array using ->toArray(), but I think it would be best to utilize the Eloquent ORM if possible.

Anyway, once you have it in the format you want, just pass it to the view like so:

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

Then, inside the your.view blade template, you can access this by using the following:

@foreach ($LinkClasses as $currentRow)
  <tr>
    <td>{{ $currentRow['id'] }}</td>
    <td>{{ $currentRow['class'] }}</td>
    <td> ... </td>
  </tr>
@endforeach

Best guess I can offer without seeing the blade template to get a better idea of what you're doing. Hope that helps!

UPDATE BASED ON OP FEEDBACK:

Since you are only receiving a single record, it seems as though the issue lies in your query. I suggest you simplify your query to fetch all records and then do your sorting within an array. Something like this in your controller:

$allClasses = DB::table('classes_lists')->all();
foreach ($allClasses as $currentClass) {
    $yearMonth = date('Y-m', $currentClass['date']);
    $classesByYearMonth[$yearMonth][] = $currentClass;
}
ksort($classesByYearMonth);

/* now you have an array of all classes sorted by year-month like 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 your blade template:

@foreach ($classesByYearMonth as $yearMonth => $classListArray)
    Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
    @foreach ($classListArray as $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 will leave it to you to fix the formatting to make your accordion work. But hopefully that will get you on the right path.




回答2:


DNoe - thank you so much. Your reply put me on exactly the right track. I had to mod some bits due to laravel ambiguities and add the strtotime but the logic was all there.

foreach ($allClasses as $currentClass) {
$ym = $currentClass['date']; 
$yearMonth = date("Y-m",strtotime($ym));
    $classesByYearMonth[$yearMonth][] = $currentClass;
}
krsort($classesByYearMonth);

return View::make('classes.index', compact('classesByYearMonth'));

The css is simple from here. I owe you some beers. And thanks for helping me take my head from my butt!

Send me a pm and i would be very very happy to forward beer donation :o

Great work and thank you again. :)




回答3:


Also, part of the problem was that the results were throwing an stdObject rather than an array.

Being able to compare your code with my own has enabled me to create a dbquery with multiple joins from which meaningfull data is selected and then converted to an array.

$classes = DB::table('table2')
->join('table1', 'table2.id', '=', 'table1.id2' )
->join('table3', 'table1.id3', '=', 'table3.id' )

->orderBy('classes_lists.date','DESC')
->get(array('table1.id', 'teacher', 'date', 'size', 'students', 'fname', 'classname', 'table1.notes'));

$cfr = count($classes);

foreach($classes as $object)
{
$arrays[] =  (array) $object;
}

foreach ($arrays as $currentClass){
$ym = $currentClass['date']; 
$yearMonth = date("Y-m",strtotime($ym));
$clazByYearMonth[$yearMonth][] = $currentClass;
}

krsort($clazByYearMonth);

This was the output into blade: Not formatted :

@foreach ($clazByYearMonth as $yearMonth => $classListArray)
Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
    @foreach ($classListArray as $currentClass)
<div>
date: {{ $currentClass['date'] }} | class: {{ $currentClass['classname'] }} | Size: {{ $currentClass['size'] }} Teacher: {{ $currentClass['fname'] }} |
</div>
    @endforeach
@endforeach


来源:https://stackoverflow.com/questions/29907542/laravel-display-a-record-details-while-grouped-by-year-or-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!