This is more of a SQL question than a Laravel one.
I'd like to accomplish something like the following based on my User model with the created_at field. (table users in database)
| created_at | user_id |
|---|---|
| 2022-04-30 | 1 |
| 2022-05-02 | 2 |
| 2022-05-03 | 4 |
| date | created_users_to_this_date | total_users_created_to_date |
|---|---|---|
| 2022-04 | 1 | 1 |
| 2022-05 | 2 | 3 |
Any idea on how to do so ?
What I have done so far (using Eloquent ORM) :
User::query()
->selectRaw("COUNT(*) created_users_to_this_date, DATE_FORMAT(created_at, '%Y-%m') date")
->orderBy('date')
->groupBy('date')
->get();
Equivalent SQL request
select COUNT(*) created_users_to_this_date, DATE_FORMAT(created_at, '%Y-%m') date from `users` where `users`.`deleted_at` is null group by `date` order by `date` asc
Thus returning
| date | created_users_to_this_date |
|---|---|
| 2022-04 | 1 |
| 2022-05 | 2 |
I thank you for your help