Laravel显示按年份或月份分组的记录详细信息 [英] Laravel Display a record details while grouped by year or month

查看:664
本文介绍了Laravel显示按年份或月份分组的记录详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚搬到了laravel框架,并开始迁移一些遗留的网站,我碰到了SQL或blade的问题 - 不知道哪个。

显示按年份和月份分组的行体育课程的负载。每个人都需要出席考勤等。

我不确定要继续的方式。
我可以显示所有行并按日期排序 - 容易挤压
我可以按年份和月份进行分组 - 但很难整理。
这些全部以手风琴形式显示。
点击月份 - 单个行下拉 - 您明白了



每月/每年可以获得多少行
我我无法弄清楚如何实际显示行。



groupBy是这样的:

  $ LinkClasses = DB :: table('classes_lists')
- > select('id,class,teacher,size')
- > select(DB :: ('年')
- 年份(日期)AS月,MONTHNAME(日期)AS month_name,COUNT(*)post_count'))
- > groupBy > groupBy('month')
- > orderBy('year','desc')
- > orderBy('month','desc')
- > orderBy ('id','desc')


解决方案

部分问题是结果抛出stdObject而不是数组。

能够将您的代码与我自己的代码进行比较,可以使用多个联接创建一个dbquery,从中选择有意义的数据,然后将其转换为数组。

  $ classes = DB :: table('table2')
- > join('table1','
- > 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($ class as $ object)
{
$ arrays [] =(array)$ object;
}

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

krsort($ clazByYearMonth);

这是刀片输出:
未格式化:

  @foreach($ clazByYearMonth as $ yearMonth => $ classListArray)
找到{{sizeof($ classListArray) }} {{$ yearMonth}}
@foreach($ classListArray as $ currentClass)
< div>
日期:{{$ currentClass ['date']}} | class:{{$ currentClass ['classname']}} |大小:{{$ currentClass ['size']}}教师:{{$ currentClass ['fname']}} |
< / div>
@endforeach
@endforeach


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')

解决方案

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

这篇关于Laravel显示按年份或月份分组的记录详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆