php - Laravel generate archive post by year and month -
i recover articles archive system ( list ) year , month laravel 4.2 .
i manage retrieve items per year , per month no problem (see below)
here controller using query builder 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, )); } and view
<ul id="show-year"> @foreach($post_links $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> i recovered data years , months adds year each month in way
2015 ( 21) - may ( 2) - april (3) - march ( 5) - february (1) - january (10) 2016 (10) - december (6) - november (4)
i don't understand why not group me in same year , generates me every time new entry of year each month.
thanks help.
for generating links in sort of navigation panel can of processing on db side , not fetching blog posts records query this
select year(created_at) year, month(created_at) month, monthname(created_at) month_name, count(*) post_count post group year, month(created_at) order year desc, month desc; output:
| year | month | month_name | post_count | ------------------------------------------ | 2013 | 5 | may | 5 | | 2013 | 4 | april | 3 | | 2013 | 3 | march | 4 | | 2013 | 2 | february | 3 | | 2013 | 1 | january | 2 | | 2012 | 12 | december | 2 | | 2012 | 11 | november | 3 | something code
$links = db::table('post') ->select(db::raw('year(created_at) year, month(created_at) month, monthname(created_at) month_name, count(*) post_count')) ->groupby('year') ->groupby('month') ->orderby('yea`enter code here`r', 'desc') ->orderby('month', 'desc') ->get(); original solution here
Comments
Post a Comment