dongping4901 2016-09-12 17:04
浏览 13
已采纳

Laravel碳组按时间段划分

I am trying to make an archive with Laravel where I can store written articles. I want this to have a structure like:

 - Today
   - Article 1
 - This week
   - Article 2
   - article 3
 - This month
   - Article 4
 - This year
   - Article 4

And when there are no articles in, for example "Today", the title "Today" is not shown. Same for any other category.

I managed to make an archive where I could store articles within a specific format, like per year:

$archives = Article::all()->groupBy(function($date) {
      return Carbon::parse($date->created_at)->format('Y');
});

As-is

but is it possible to achive the list I gave above with some changes to the code I have now or with some other code?

  • 写回答

1条回答 默认 最新

  • dsfo22654 2016-09-12 17:57
    关注

    I think using MySQL, we can make it fairly elegant (although it does require a DB::raw()). Also it's pretty easy to make changes to.

    $groupSelect =
    "CASE
        WHEN DAYOFYEAR(created_at) = DAYOFYEAR(CURDATE()) THEN 'Today'
        WHEN WEEKOFYEAR(created_at) = WEEKOFYEAR(CURDATE()) THEN 'This Week'
        WHEN DATE_FORMAT(created_at, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') THEN 'This Month'
        WHEN YEAR(created_at) = YEAR(CURDATE()) THEN 'This Year'
        ELSE 'Super Duper Long Time Ago'
    END AS theGroup";
    
    $archives = Article::select('*')->addSelect(DB::raw($groupSelect))->get()->groupBy(function($article) {
        return $article->theGroup;
    });
    

    If you don't like the raw SQL you can add a function to your Article model which will read the date and determine if it's Today, This Week, This Month, or This Year and returns that string...

    public function getDiffForArchive()
    {
        if ( strtotime($this->attributes['created_at']) > strtotime('-1 day')) {
            return 'Today';
        } else if (...) {  // Continue on with above logic.
            return '...';
        }
    }
    

    Then in your callback it's as simple as...

    return $article->getDiffForArchive();
    

    Edit

    I've updated the SQL because I think it's close to what you want and also needed to start with select('*) on your query builder, otherwise it will only select the theGroup. It works now though, I've tested it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入