Is there a way i can track a logged in user activity in Laravel without using any packages? I have tried using antonioribeiro/tracker package but doesn't have a clear read me manual. I want to know details like the pages visited by a user. PS: For a small project i usually create a simple logPageHit() function that i place on the necessary Controller methods but right now am dealing with a big project
Asked
Active
Viewed 1.1k times
0
-
there are many ways if you try to write, or use online tracking systems like google and etc... – Mohammad Fanni Aug 09 '17 at 13:58
2 Answers
1
You could use a Log package with events,with example please follow this link
OR Laravel Eloquent have many events for manage it
Hope this work for you!!!

AddWeb Solution Pvt Ltd
- 21,025
- 5
- 26
- 57
-
Thanks @AddWeb Solution Pvt Ltd. However i am trying not to use any package. – brightniyonzima Aug 11 '17 at 06:02
1
Tracking If a User Is Currently Online
You can create a Middleware
php artisan make:middleware LastUserActivity
Inside the handle method, we need to add the following code:
if(Auth::check()) {
$expiresAt = Carbon::now()->addMinutes(5);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}
Go to App\Http\Kernel.php
add the above code, inside $middlewareGroups in the web section.
\App\Http\Middleware\LastUserActivity::class,
Note: It is important that it’s added after the StartSession middleware, otherwise, the Auth facade will not have access to the logged in user.
Go to App\User.php and add this method.
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
Now use this in any of your views.
@if($user->isOnline())
user is online!!
@endif
Hope it helps. Read this tutorial for more info.

diakosavvasn
- 854
- 1
- 8
- 22
-
Thanks @Nikolas. However this isn't what am exactly looking for. I realised maybe i was too broad so i have edited my question to be clearer. – brightniyonzima Aug 11 '17 at 06:06