获取所有连接的用户(Laravel 5) [英] Get All Connected Users (Laravel 5)
问题描述
如何显示我家中所有已连接的用户?
How to display all connected users in my home?
推荐答案
这是一种非常基本的方法,但希望能有效地检测Laravel5应用程序中的Guest
和Registered
用户.
This is a very basic, but hopefully an efective way to detect both Guest
and Registered
users on your Laravel5 application.
第1步
打开文件config/session.php
,并将驱动程序更改为数据库.
Open the file config/session.php
and change the driver to database.
第2步
我们需要创建会话表,因此请使用以下工匠命令php artisan session:table
生成迁移文件.
We need to create the sessions table, so use the following artisan command php artisan session:table
to generate the migration file.
第3步
在此新生成的迁移中,您需要添加一个新的user_id
列,这样我们可以将会话与用户相关联(如果该用户已登录).
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.
打开文件migrations/xxxx_xx_xx_xxxxxx_create_session_table.php
,然后在Schema :: create中添加以下内容:
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();
这是完整迁移的外观:
<?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');
}
}
第4步
运行composer dump-autoload
和php artisan migrate
.
注意:如果尚未全局安装Composer,则只需使用php composer.phar dump-autoload
.
第5步
将Eloquent模型保存为应用程序中的Session.php
.
Save the Eloquent Model somewhere on your application as Session.php
.
注意:建议将此位置保存在应用程序目录中.
第6步
现在,您只需要知道如何使用它.
Now you just need to know how to use it.
. .
用法
将以下Session::updateCurrent();
放置在代码中,因为这将确保当前用户的会话条目已更新,仅作为示例,您可以将其放置在app/routes.php文件中.
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();
如果您需要在一定时期内(例如10分钟)在线检查所有用户,则需要调用activity(:limit)
方法,如下所示:
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();
注意:此方法可以与guest()和/或registered()方法结合使用.
Note: This method can be used in combination with the guests() and/or registered() methods.
访客用户
全部获取
$guests = Session::guests()->get();
Get the # of Guest users
$total = Session::guests()->count();
注册用户
全部获取
$registered = Session::registered()->get();
foreach ($registered as $online) {
// You can retrieve the user information using something like:
var_dump($online->user->email);
}
获取注册用户数
$total = Session::registered()->count();
口才模式:
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.
这篇关于获取所有连接的用户(Laravel 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!