TAY
笔记 · · 阅读 276

laravel 按年-月分组列出文章

$articles = Article::orderbydesc('created_at')->paginate(20)->toArray();
         
// 按月分组处理
function archives_document($arrData) {
    $rv = array();
    foreach ($arrData as $index =>$value) {
        $archivesTime = date("Y-m", strtotime($value['created_at']));
        $rv[$archivesTime][] = (object)$value;
    }
    return $rv;
}
// 获取分组后的数据
$articles['archives'] = archives_document($articles['data']);
目录