Below is the image is what i am trying to achieve:-
I am on Laravel and I want to get the last 30 days from the current date data from the user table based on how many new users enter a website and how many users delete the account from the website. I have managed the deleted user with soft delete. I have to show that data in the chat.js. I have done a below code but not getting the proper data for my chat.js
$startDate = now()->subDays(30)->startOfDay();
$endDate = now()->endOfDay();
$days = [];
$currentDate = clone $startDate;
$allUsers = User::count();
while ($currentDate->lte($endDate)) {
$days[$currentDate->format('M d')] = $allUsers;
$currentDate->addDay();
}
$records = User::withTrashed()
->whereBetween('created_at', [$startDate, $endDate])
->select('created_at', 'deleted_at')
->get();
foreach ($records as $record) {
$date = Carbon::parse($record->created_at)->format('M d');
if($allUsers != 0){
$days[$date] += $record->deleted_at ? -1 : 1;
}
}
$labels = array_keys($days);
$data = array_values($days);
return [
'datasets' => [
[
'label' => 'New user',
'data' => $data,
],
],
'labels' => $labels,
];
I have managed to get the data but generated data is not correct. I want to show the last 30 days in chat.js For example if today is 14-jul-23 to i want to get data between 14-jun-23 to 14-jul-23. and also if before 14-jun-23 there is 5 active user in the user table and some new user enters between the last 30 days then the count must start with 6.
This is on my user table screenshort
And this is the output output screenshot