Session Laravel会话表添加附加列

Session Laravel会话表添加附加列,session,laravel,laravel-4,logout,Session,Laravel,Laravel 4,Logout,我想在session表中添加一个额外的列user\u id 原因是,有时会有垃圾邮件发送者注册假帐户,一旦我知道该用户是垃圾邮件发送者,我想通过删除会话记录将该用户注销 有可能实现这一点吗?这是会话迁移模式: Schema::create('sessions', function($table) { $table->string('id')->unique(); $table->text('payload'); $table->integer('l

我想在
session
表中添加一个额外的列
user\u id

原因是,有时会有垃圾邮件发送者注册假帐户,一旦我知道该用户是垃圾邮件发送者,我想通过删除会话记录将该用户注销


有可能实现这一点吗?

这是会话迁移模式:

Schema::create('sessions', function($table)
{
    $table->string('id')->unique();
    $table->text('payload');
    $table->integer('last_activity');
    $table->integer('user_id')->unsigned(); //// ADD IT!
});
Schema::create('sessions', function (Blueprint $table) {
        $table->string('id')->unique();
        $table->integer('user_id')->nullable();
        $table->string('ip_address', 45)->nullable();
        $table->text('user_agent')->nullable();
        $table->text('device')->nullable();
        $table->text('payload');
        $table->integer('last_activity');
    });
你可以在上面加任何你喜欢的栏目,拉威尔不会介意的

或者,您可以创建一个新的迁移,并使其在该表上添加一列

$table->integer('user_id')->unsigned();
可以为其创建模型:

class SessionModel extends Eloquent {

}
然后用它做任何你需要的事情:

$session = SessionModel::find(Session::getId());
$session->user_id = 1;
$session->save();

但是,如果您正在考虑向负载中添加更多信息,这也是Laravel保存会话数据的地方,尽管我认为这是可能的,但您必须在Laravel的代码中挖掘更多信息才能做到这一点。

迁移的简短版本

if( Schema::hasTable('sessions') ) {
    $table->integer('user_id')->unsigned();
}
需要检查是否存在
会话
表。或者在以下时间之前创建它:

php artisan session:table
php artisan migrate

这是会话迁移模式:

Schema::create('sessions', function($table)
{
    $table->string('id')->unique();
    $table->text('payload');
    $table->integer('last_activity');
    $table->integer('user_id')->unsigned(); //// ADD IT!
});
Schema::create('sessions', function (Blueprint $table) {
        $table->string('id')->unique();
        $table->integer('user_id')->nullable();
        $table->string('ip_address', 45)->nullable();
        $table->text('user_agent')->nullable();
        $table->text('device')->nullable();
        $table->text('payload');
        $table->integer('last_activity');
    });
要在向表中添加列之后向有效负载添加信息,只需更改DatabaseSessionHandler类

路径:vendor/laravel/framework/src/illumb/Session

例:

1º创建函数

protected function device()
{
    $agent = new \Jenssegers\Agent\Agent;

    if ($agent->isDesktop()){
        $device = 'desktop';
    }

    return $device;
}
2º插件函数addRequestInformation(&$payload)


准备好了,用户登录时将设备添加到表中。

@Antonio Carlos Ribeiro谢谢。我的情况也一样。我有一个疑问,你们能让我知道吗,我计划一次从一个位置锁定登录,所以我将继续使用数据库身份验证/会话驱动程序。因此,从另一个位置使用相同的凭证登录会显示一条消息“已经从另一个位置登录”。这种方法行吗?感谢我在尝试保存会话时遇到此错误
SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“updated_at”
@EdmundSulzanok如果为会话创建模型类,则需要确保包含
public$timestamps=false。否则,您的模式需要包括
$table->timestamps()