it's hard to tell if this is behaving the way we want it to
How will you validate the answer(s) you'll get on this forum?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am working to get some specific analytic data for each fiscal quarter for our application, and I am not sure if the code is getting the right values. The data looks like this:
| ID | UUID | City | County | Zip | IP | session_id | deviceID | created_at |
| 1 | 1234 | city | county | 0 | 127.0.0.1 | abcd | string_1 | 2022-06-01 |
| 2 | 1235 | city | county | 0 | 127.0.0.1 | efgh | string_1 | 2022-06-01 |
| 3 | 4324 | city | county | 0 | 127.0.0.1 | ffsd | string_2 | 2022-06-01 |
| 4 | 2213 | city | county | 0 | 127.0.0.1 | a441 | string_2 | 2022-06-01 |
Basically, it's a log of users that access the system and some meta information. We count each unique visit with the UUID, and then we count the session IDs, and the device IDs. Device IDs are a cookie that is stored for the maximum time, so it's not perfect but it accomplishes what we want for most cases.
What I am trying to access is this:
So for this example, the total records are 4, and the unique records are 2. The unique per day is 2 and total per day is 1, since all records are for the same date. But if there were more dates, I would have the total and unique counts for each date in the range defined.
The code I have for this is here:
$currentTotal = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->count();
$currentUnique = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->groupBy('deviceID')->count();
$dailyTotal = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->selectRaw('count(*) as count, DATE(created_at) as date')
->groupBy('date')
->get()
->toArray();
$dailyUnique = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->selectRaw('count(distinct deviceID) as count, DATE(created_at) as date')
->groupBy('date')
->get()
->toArray();
I am testing this with some old data, and it's hard to tell if this is behaving the way we want it to.
After running the code against some custom test data, I realized that I have to use Distinct instead of GroupBy but otherwise it works.
Here is the final code:
$currentTotal = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->count();
$currentUnique = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->distinct('deviceID')->count();
$dailyTotal = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->selectRaw('count(*) as count, DATE(created_at) as date')
->groupBy('date')
->get()
->toArray();
$dailyUnique = AccessRecord::whereBetween(
'created_at',
[$currentQuarterDates[$currentQuarter]['start'], $today]
)->selectRaw('count(distinct deviceID) as count, DATE(created_at) as date')
->groupBy('date')
->get()
->toArray();
Please or to participate in this conversation.