Post::query()
->selectRaw("DAYNAME(created_at) as day, COUNT(id) as number")
->groupByRaw("DAYNAME(created_at)")
->get();
Mar 3, 2022
32
Level 5
How to get data of this week?
How can I get a records count of each day of this week?
I need to make my response something like this:
{
"day":"sunday",
"number":1,
},
{
"day":"monday",
"number":4,
},
{
"day":"tuesday",
"number":5,
},
{
"day":"wednesday",
"number":46,
},
{
"day":"thursday",
"number":86,
},
{
"day":"friday",
"number":87,
},
{
"day":"saturday",
"number":10,
}
how many records of posts for each day of this week!
My query (wrong)
$posts = Post::query()
->whereBetween('created_at', [
now()->startOfWeek(Carbon::SATURDAY)->format('Y-m-d'),
now()->endOfWeek(Carbon::FRIDAY)->format('Y-m-d')
])
->count();
dd($posts);
Level 104
@Jean_ali that is more awkward since there is no data to count/group. With mysql, there is no function like Postgres' generate_series; so we need to fake it. And there are a number of approaches to solving this.
https://stackoverflow.com/questions/2157282/generate-days-from-date-range
1 like
Please or to participate in this conversation.