Because I am old school, I am in the habit of first writing my queries in SQL, and then converting them to QB. So, the below works fine. The objective is to count the number of child_id for every given date between start and end.
SELECT
date,
COUNT(child_id)
FROM
attendance
WHERE (date BETWEEN '2021-01-01' AND '2021-01-12 ')
GROUP BY date;
However, when I attempt to move it to QB based on advice from SO,
( https://stackoverflow.com/questions/13223512/how-to-select-count-with-laravels-fluent-query-builder )
I get an error:
$dates = DB::table('attendance')
->select(array('date', DB::raw('COUNT(child_id)')))
->where('date', '>=', $this->startDate)
->where('date', '<=', $this->endDate)
->groupBy('date')
->get();
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in 'field list' (SQL: select `date`, `count` from `attendance` where `date` >= 2021-01-01 and `date` <= 2021-01-12 order by `date` desc)
Where am I going wrong?