Brijpal Sharma
laravel
Publish on:-March 07th, 2019 ,
Category:- Web Development
Brijpal Sharma
laravel
Publish on:-March 07th, 2019 ,
Category:- Web Development
Some developers use a token to store in the users' table when the user login and deletes token when the user logs out but what happens when the user, not logs out but closes the tab or maybe not on PC or system.
Here use a proper method to use know user online or not we also seta time duration if the user does not do any activity duration set time. so let's start
If you work on an existing project you can skip this process.
Create a new Laravel project using this command.
laravel new userOnline
In the second step we setup database connection in the .env file than migrate table using this command.
php artisan migrate
After that fill some dummy data in the user table to check users online or not in Laravel.
Create a middleware LastUserActivity using this command.
php artisan make:middleware LastUserActivity
Add some code check user online or not
App\Http\Middleware\LastUserActivity .php
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use Cache;
use Carbon\Carbon;
class LastUserActivity
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check()) {
$expiresAt = Carbon::now()->addMinutes(1);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}
return $next($request);
}
}
Add a class into Kernal file in middlewareGroups
\App\Http\Middleware\LastUserActivity::class,
full code of $middlewareGroups
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\LastUserActivity::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
Ok, We almost are done now time to check.
Don't forget to add use Cache in User Model At the top;
use Cache;
Use the isOnline function in view.
@if($user->isOnline())
user is online!!
@endif
Brijpal Sharma
Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...
Amit Rajput
Publish on:-September 20th, 2019
Superb Boss and thanks to you for the very imported posts.
My name is Amit Rajput
I am a Php developer
my experience is 2yr.
I have knowledge laravel, Codeigniter.
Sahm
Publish on:-April 06th, 2020
Got this error
Undefined variable for user
Barry Diouma
Publish on:-July 14th, 2020
the user variable must be in your controller.
example:
$ all_user = User :: all ();
now in your view make a foreach of $ all_user as $ user
Khaled Ahmed
Publish on:-September 14th, 2020
I used here Laravel 7 and Error
Call to a member function addEagerConstraints() on bool
what is the problem I do not understand
You must be logged in to post a comment.