Laravel Eloquent get results grouped by days

前端 未结 14 593
一整个雨季
一整个雨季 2020-12-02 06:44

I currently have a table of page_views that records one row for each time a visitor accesses a page, recording the user\'s ip/id and the id of the page itself. I should add

14条回答
  •  -上瘾入骨i
    2020-12-02 07:30

    You can use Carbon (integrated in Laravel)

    // Carbon
    use Carbon\Carbon;   
    $visitorTraffic = PageView::select('id', 'title', 'created_at')
        ->get()
        ->groupBy(function($date) {
            return Carbon::parse($date->created_at)->format('Y'); // grouping by years
            //return Carbon::parse($date->created_at)->format('m'); // grouping by months
        });
    

提交回复
热议问题