How to get all users logged in at the moment in Laravel 5.4

0

I need to loop with all the users who are currently logged into the system. How could I do that? I know that with auth()->check() we can know if a user is logged in. Plus I wanted to know everyone who is logged in right now. Is there a way?

    
asked by anonymous 19.10.2017 / 23:34

1 answer

0

This is a very basic way, but I hope it will be effective.

Step 1

Open the file config/session.php and change the driver to the database.

Step 2

We need to create the session table, so use the following command artisan -   php artisan session:table to generate the migration file.

Step 3

On this newly generated migration, you need to add a new column user_id , so we can relate 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 within Schema::create :

$t->integer('user_id')->nullable();

Here is how% complete% must be:

<?php

use Illuminate\Database\Migrations\Migration;

class CreateSessionTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sessions', function($t) 
        {
            $t->string('id')->unique();
            $t->text('payload');
            $t->integer('last_activity');
            $t->integer('user_id')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('sessions');
    }

}

Step 4

Run migration and composer dump-autoload .

Note: If you do not have Composer installed globally, use only php artisan migrate .

Step 5

Save the Model php composer.phar dump-autoload somewhere in your application as Eloquent .

Note: The recommended location to save this is in the application directory.

Step 6

Now you just need to know how to use it.

. . .

Use

Place the following Session.php somewhere in your code, as this will ensure that the session entry for the current user is updated, just an example, you can put it in your Session::updateCurrent(); file. >

Get all users

$all = Session::all();

If you need to check all users online for a certain time, such as 10 minutes, you need to call the app/routes.php method, like this:

$all = Session::activity(10)->get();

Note: This method can be used in combination with the guest () and / or registered () methods.

Guest Users

Take all

$guests = Session::guests()->get();
Get the # of Guest users

$total = Session::guests()->count();

Registered Users

Take all

$registered = Session::registered()->get();

foreach ($registered as $online) {
    // You can retrieve the user information using something like:
    var_dump($online->user->email);
}

Get the number of registered users

$total = Session::registered()->count();

Eloquent Template

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Database\Eloquent\Builder;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;

class Session extends Model 
{
    /**
     * {@inheritdoc}
     */
    public $table = 'sessions';

    /**
     * {@inheritdoc}
     */
    public $timestamps = false;

    /**
     * Returns the user that belongs to this entry.
     *
     * @return \Cartalyst\Sentinel\Users\EloquentUser
     */
    public function user()
    {
        return $this->belongsTo('Cartalyst\Sentinel\Users\EloquentUser');
    }

    /**
     * Returns all the users within the given activity.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  int  $limit
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeActivity($query, $limit = 10)
    {
        $lastActivity = strtotime(Carbon::now()->subMinutes($limit));

        return $query->where('last_activity', '>=', $lastActivity);
    }

    /**
     * Returns all the guest users.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeGuests(Builder $query)
    {
        return $query->whereNull('user_id');
    }

    /**
     * Returns all the registered users.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeRegistered(Builder $query)
    {
        return $query->whereNotNull('user_id')->with('user');
    }

    /**
     * Updates the session of the current user.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeUpdateCurrent(Builder $query)
    {
        $user = Sentinel::check();

        return $query->where('id', Session::getId())->update([
            'user_id' => $user ? $user->id : null
        ]);
    }
}

Alternatively, you can try this .

    
20.10.2017 / 15:39