【发布时间】:2016-05-18 16:34:36
【问题描述】:
我现在将使用 Laravel 4.2 的归档系统(列表)恢复我的文章。
我设法每年和每月都可以毫无问题地取回我的物品(见下文)
这是我使用查询构建器 Laravel 的控制器
public function index(){
$post_links = DB::table('posts')
->select(DB::raw('YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name, COUNT(*) post_count'))
->groupBy('year')
->groupBy('month')
->orderBy('year', 'desc')
->orderBy('month', 'desc')
->get();
return View::make('home.index')->with(array(
'post_links'=>$post_links,
));
}
我的看法
<ul id="show-year">
@foreach($post_links as $link)
{{--{{dd($link)}}--}}
{{--Show year--}}
<li>
<a title="" href="#">
<span>
<strong>{{$link->year}}</strong></a> <span class="post-count">{{$link->post_count}}</span><i class="fa fa-arrow-right"></i>
</span>
</li>
</a>
{{--show month--}}
<ul id="show-month">
<li class="month-content">
<a href="">
<span>
<strong>{{$link->month_name}}</strong>
</span>
</a>
</li>
</ul>
</li>
@endforeach
</ul>
我恢复了我的数据年和月,但它以这种方式每个月为她增加了一年
2015 (21) - 五月(2) - 四月 (3) - 三月(5) - 二月 (1) - 一月 (10) 2016 (10) - 十二月 (6) - 11 月 (4)
我不明白为什么它不在同一年对我进行分组,并且每次都会为我生成每个月的新年份条目。
感谢您的帮助。
【问题讨论】:
-
试试这个方法 ->groupBy(['year', 'month'])
-
当我调试 $posts_links 时,我有一个对象数组,如该数组 (size=4) 0 => object(stdClass)[362] public 'year' => string '2016' (length= 4) public 'month' => string '1' (length=1) public 'month_name' => string 'January' (length=7) public 'post_count' => string '1' (length=1) 我的年份不分组
-
我怎样才能 foreach 有一个带有月份的年份数组
标签: php mysql laravel-4 eloquent