Skip to content

Commit 98c9ee2

Browse files
authored
Merge pull request #13 from coderflexx/enhancements
Enhancement
2 parents 001c6a4 + 1c7db54 commit 98c9ee2

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/PendingVisit.php

+19-14
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,25 @@ protected function shouldBeLoggedAgain(Visit $visit): bool
110110

111111
public function __destruct()
112112
{
113-
if (! $this->isCrawler) {
114-
$visit = $this->model
115-
->visits()
116-
->latest()
117-
->firstOrCreate($this->buildJsonColumns(), [
118-
'data' => $this->attributes,
119-
]);
120-
121-
$visit->when(
122-
$this->shouldBeLoggedAgain($visit),
123-
function () use ($visit) {
124-
$visit->replicate()->save();
125-
}
126-
);
113+
// if the current request is a crawler,
114+
// we don't need to log the visit
115+
// because it's not a real visitor
116+
if ($this->isCrawler) {
117+
return null;
127118
}
119+
120+
$visit = $this->model
121+
->visits()
122+
->latest()
123+
->firstOrCreate($this->buildJsonColumns(), [
124+
'data' => $this->attributes,
125+
]);
126+
127+
$visit->when(
128+
$this->shouldBeLoggedAgain($visit),
129+
function () use ($visit) {
130+
$visit->replicate()->save();
131+
}
132+
);
128133
}
129134
}

tests/Feature/Visits/VisitsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,5 @@
225225
request()->headers->set('User-Agent', 'Googlebot');
226226
$post->visit();
227227

228-
expect($post->visits->first())->toBe(null);
228+
expect($post->visits->count())->toBe(0);
229229
});

0 commit comments

Comments
 (0)