Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
@dontspamagain
Copy link

dontspamagain commented Mar 4, 2014

Sorry, I thought it was possible to pull a request but this is not the case apparently.

"" missed, and we can't use : "use Sentry; use Session;"

ps: work with Sentry 2

@brunogaspar
Copy link
Author

brunogaspar commented Mar 4, 2014

Thanks, i've updated the Gist to have both Sentry 2 and Sentry 3 (default) models.

@lhasbrou
Copy link

lhasbrou commented Jul 16, 2015

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