【问题标题】:Laravel generate archive post by year and monthLaravel 按年和月生成归档帖子
【发布时间】: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>&nbsp;<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


【解决方案1】:

为了在某种导航面板中生成链接,您可以在数据库端进行大部分处理,而不是使用这样的查询获取所有博客文章记录

SELECT YEAR(created_at) year,
       MONTH(created_at) month,
       MONTHNAME(created_at) month_name,
       COUNT(*) post_count
  FROM post
 GROUP BY year, MONTH(created_at)
 ORDER BY year DESC, month DESC;

输出:

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

类似这样的代码

  $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();

原来的解决方案是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多