0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
brightniyonzima
  • 9
  • 1
  • 1
  • 5

2 Answers2

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
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