Laravel 5.0 show registered users and registered users
I am trying to add a function to my site that shows the number of registered users and registered users (also showing the names of registered users, but I can figure out this part). I currently read that I need to modify the /config/Session.php "driver" file to use database
instead of file
( 'driver' => env('SESSION_DRIVER', 'database'),
). I found an article that uses Sentry to accomplish this ( http://laravel.io/forum/03-03-2014-sentry-3-users-online ) however I would prefer to do it without requiring any third party files. However, I used my information as a starting point to use the model for the session.
As far as problems go, first, I can't actually get the session to start saving to my database sessions table even after changing Session.php, creating the table and migrating it. It is still saved in files in / storage / framework / session / (I can still log in and display session information with Session::all()
).
mysql> select * from sessions;
Empty set (0.00 sec)
Second problem, I don't know how I should create a session, if it does not already exist in its file, there is no way to create (if someone just opens the site and is thus a guest) and unregister the user on logout (remove your registered status as well as its user_id so they can be seen as a guest).
Here is my current OnlineUser.php file:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
// use Sentry;
use Session;
use Auth;
class OnlineUser 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' => !empty(Auth::user()) ? Auth::user()->id : null
));
}
/**
* Returns the user that belongs to this entry.
*
* @return \Cartalyst\Sentry\Users\EloquentUser
*/
public function user()
{
return $this->belongsTo('\App\User');
//return $this->belongsTo('Cartalyst\Sentry\Users\EloquentUser'); # Sentry 3
// return $this->belongsTo('Cartalyst\Sentry\Users\Eloquent\User'); # Sentry 2
}
}
If anyone has any information to help me with these issues, I would greatly appreciate it. If you need more information, let me know and I will reply when I can.
Thank.
source to share