Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kinger's avatar

Select average daily number of rows with Laravel

Hello there,

I want to select the average daily number of rows based on created_at column. I want to achieve that with Laravel query scopes. Any suggestions?

protected function scopeDaily(Builder $query): void
    {
        $query
            ->selectRaw('DATE(created_at) as date, COUNT(*) as daily_count')
            ->groupBy('date');
    }

    protected function scopeDailyAverage(Builder $query): void
    {
        // here I want to apply the average method
    }

Here is what I have so far.

Thank you

0 likes
1 reply
aletopo's avatar

@kinger

Try with Eloquent

        $query
            ->avg('created_at')
            ->groupBy('created_at');

Please or to participate in this conversation.