Sai Krishna
Sai Krishna

Reputation: 557

Laravel Eloquent grouping by days of the week

I'm currently using this

 Bills::select( \DB::raw('DATE(`created_at`) as `date`'),\DB::raw('COUNT(*) as `count`'))->whereBetween('created_at', [Carbon\Carbon::parse('last sunday')->startOfDay(),Carbon\Carbon::parse('next monday')->endOfDay(), ]) ->groupBy('date')->get() ;

but is there better Eloquent query to search within the calendar week and group the daily count by days?

Upvotes: 3

Views: 2986

Answers (2)

Bogdan F
Bogdan F

Reputation: 5

One more thing: in your applications's database config file config/database.php

Then mysql array, set strict => false to disable MySQL's strict mode

Upvotes: 0

Mr. Pyramid
Mr. Pyramid

Reputation: 3935

Yes you have better version in eloquent

$day1 = Carbon::parse('last sunday')->startOfDay();
        $day2 = Carbon::parse('next monday')->endOfDay();

        $bills = Bills::whereBetween('created_at',$day1,$day2)->groupBy('created_at')->get();
        $count = $bills->count();

Upvotes: 1

Related Questions