Laravel Eloquent get results grouped by days

前端 未结 14 502
一整个雨季
一整个雨季 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条回答
  • 2020-12-02 07:27

    Here is how I do it. A short example, but made my query much more manageable

    $visitorTraffic = PageView::where('created_at', '>=', \Carbon\Carbon::now->subMonth())
                            ->groupBy(DB::raw('Date(created_at)'))
                            ->orderBy('created_at', 'DESC')->get();
    
    0 讨论(0)
  • 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
        });
    
    0 讨论(0)
  • 2020-12-02 07:31

    I built a laravel package for making statistics : https://github.com/Ifnot/statistics

    It is based on eloquent, carbon and indicators so it is really easy to use. It may be usefull for extracting date grouped indicators.

    $statistics = Statistics::of(MyModel::query());
    
    $statistics->date('validated_at');
    
    $statistics->interval(Interval::$DAILY, Carbon::createFromFormat('Y-m-d', '2016-01-01'), Carbon::now())
    
    $statistics->indicator('total', function($row) {
        return $row->counter;
    });
    
    $data = $statistics->make();
    
    echo $data['2016-01-01']->total;
    

    ```

    0 讨论(0)
  • You could also solve this problem in following way:

    $totalView =  View::select(DB::raw('Date(read_at) as date'), DB::raw('count(*) as Views'))
            ->groupBy(DB::raw('Date(read_at)'))
            ->orderBy(DB::raw('Date(read_at)'))
            ->get();
    
    0 讨论(0)
  • 2020-12-02 07:32

    I believe I have found a solution to this, the key is the DATE() function in mysql, which converts a DateTime into just Date:

    DB::table('page_views')
          ->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as views'))
          ->groupBy('date')
          ->get();
    

    However, this is not really an Laravel Eloquent solution, since this is a raw query.The following is what I came up with in Eloquent-ish syntax. The first where clause uses carbon dates to compare.

    $visitorTraffic = PageView::where('created_at', '>=', \Carbon\Carbon::now->subMonth())
                                ->groupBy('date')
                                ->orderBy('date', 'DESC')
                                ->get(array(
                                    DB::raw('Date(created_at) as date'),
                                    DB::raw('COUNT(*) as "views"')
                                ));
    
    0 讨论(0)
  • 2020-12-02 07:38

    in mysql you can add MONTH keyword having the timestamp as a parameter in laravel you can do it like this

    Payement::groupBy(DB::raw('MONTH(created_at)'))->get();
    
    0 讨论(0)
提交回复
热议问题