How to display all connected users in my home?
相关问题
- java client program to send digest authentication
- PHP persistent login - Do i reissue a cookie after
- How to execute MYSQL query in laravel?
- How to handle “App is temporarily blocked from log
- How can I add condition on mail notification larav
相关文章
- User.Identity.IsAuthenticated vs WebSecurity.IsAut
- Laravel 5 input old is empty
- SwiftUI - Vertical Centering Content inside Scroll
- Override UserManager in django
- Laravel dusk not working .env.dusk.local
- Your application has authenticated using end user
- Read a .csv file into an array in Laravel
- Laravel Impossible to Create Root Directory
This is a very basic, but hopefully an efective way to detect both
Guest
andRegistered
users on your Laravel5 application.Step 1
Open the file
config/session.php
and change the driver to database.Step 2
We need to create the sessions table, so use the following artisan command
php artisan session:table
to generate the migration file.Step 3
On this newly generated migration, you need to add a new
user_id
column, this is so we can relate the session to a user, if that user is logged in of course.Open the file
migrations/xxxx_xx_xx_xxxxxx_create_session_table.php
and add the following inside the Schema::create:$t->integer('user_id')->nullable();
Here is how the full migration should look:
Step 4
Run
composer dump-autoload
andphp artisan migrate
.Note: If you don't have Composer installed globally, just use
php composer.phar dump-autoload
.Step 5
Save the Eloquent Model somewhere on your application as
Session.php
.Note: The recommended place to save this is on the app directory.
Step 6
Now you just need to know how to use it.
. . .
Usage
Place the following
Session::updateCurrent();
somewhere on your code, as this will make sure that the session entry for the current user get's updated, just an example, you can place it on your app/routes.php file.Get all users (Guests + Registered)
$all = Session::all();
If you need to check all users online for a certain period, like 10 minutes, you need to call the
activity(:limit)
method, like so:$all = Session::activity(10)->get();
Note: This method can be used in combination with the guests() and/or registered() methods.
Guest Users
Grab all
Registered Users
Grab all
Get the # of Registered users
$total = Session::registered()->count();
Eloquent model:
Alternatively you can try this.