Skip to content

Instantly share code, notes, and snippets.

@brunogaspar
Last active October 13, 2023 00:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save brunogaspar/9324532 to your computer and use it in GitHub Desktop.
Save brunogaspar/9324532 to your computer and use it in GitHub Desktop.
Sentry 3 Users Online

Step 1

Open the file app/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 an user if that user is logged in.

Open the file app/migrations/xxxx_xx_xx_xxxxxx_create_session_table.php and add the following inside the Schema::create

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

Here is mine

<?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 php artisan dump-autoload and php artisan migrate.

Step 5

Create a new model called Online.php (you can rename it if you want) with the following :

<?php

use Illuminate\Database\Eloquent\Model;
// use Sentry;
// use Session;

class Online extends Model {

	/**
	 * {@inheritDoc}
	 */
	public $table = 'sessions';

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

	/**
	 * Returns all the guest users.
	 *
	 * @param  \Illuminate\Database\Eloquent\Builder  $query
	 * @return \Illuminate\Database\Eloquent\Builder
	 */
	public function scopeGuests($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($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($query)
	{
		return $query->where('id', Session::getId())->update(array(
			'user_id' => Sentry::check() ? Sentry::getUser()->id : null
		));
	}

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

}

How to Use

You need to place this Online::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.

Getting all the Guest users

$guests = Online::guests()->get();

Getting the # of Guest users

$totalGuests = Online::guests()->count();

Getting the Registered users

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

Getting the # of Registered users

$totalRegistered = Online::registered()->count();

Getting a registered user information while looping

foreach ($registered as $online)
{
	var_dump($online->user->email);
}
@lhasbrou
Copy link

Must one use Sentry in order to make use of this? I am simply trying to show online registered and guest users and don't want to have to require more third party files if at all possible. Also, I do not see anywhere that a new session is created nor deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment