I am currently working on a web application and using ChartJS. I need to get data from the last 7 days and count it. I explain:
I need to count the number of client that registered each days from the seven last days to display it with ChartJS.
I have some problems with the SQL Request.
Get today's date:
$jj = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
In my database, i have a 'created_at' column with timestamp like this :
2018-08-21 11:07:59
For the request, I followed some tutorials and did this:
SELECT count(client.id) FROM parcel WHERE created_at = :dateclient, array('dateclient' => $jj);
But the request isn't working, I assume that there are some errors. If some pros knows how to do it !
By the way: I did the request for one date for the moment, but I will need to do it for seven days
Please use eloquent query to overcome SQL Injection like following
Here I am assuming you have Models folder inside App folder else you can directly use Parcel:: without Models\Parcel
$lastWeek = date('Y-m-d', strtotime("-7 day", date('Y-m-d')));
/* In the following code you can directly get the count value */
$parcelCount = Models\Parcel::whereDate('created_at', $lastWeek)->count();
OR using DB::raw
$parcelCount = Models\Parcel::select(\DB::raw("COUNT(id) as parcel_count"))
->whereDate('created_at', $lastWeek)
->first();