Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
Fix: the groupBy() method works improperly when combined with WHERE
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMordred committed Feb 9, 2018
1 parent ffd96ab commit 020f8bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ protected function groupedVisits($view, $groupBy, $subtitle)
->visits()
->withUsers()
->latest()
->groupBy($groupBy)
->except(['ajax', 'bots', 'login_attempts'])
->groupBy($groupBy)
->paginate(config('visitortracker.results_per_page', 15)),
'visitortrackerSubtitle' => $subtitle,
], $this->viewSettings()));
Expand Down
4 changes: 4 additions & 0 deletions src/VisitStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public function groupBy($field)

$this->sqlSelect .= ', v2.visits_count, v2.visitors_count';

$where = str_replace('v.', '', $this->sqlWhere());

$this->sql .= "
JOIN
(
Expand All @@ -170,6 +172,8 @@ public function groupBy($field)
COUNT(*) AS visits_count,
COUNT(DISTINCT ip) AS visitors_count
FROM visitortracker_visits
{$where}
GROUP BY {$field}
) v2
ON v2.{$field} = v.{$field}
Expand Down
4 changes: 3 additions & 1 deletion tests/StatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ public function testGetVisitsGroupedByCountry()
$results = VisitStats::query()->visits()->groupBy('country_code')->get();

$this->assertCount(4, $results);

$this->assertEquals(6, $results[0]->visits_count);
$this->assertEquals(5, $results[1]->visits_count);
$this->assertEquals(1, $results[0]->visitors_count);

$this->assertEquals(5, $results[1]->visits_count);
$this->assertEquals(1, $results[1]->visitors_count);
}

Expand Down

0 comments on commit 020f8bb

Please sign in to comment.