Laravel会话表添加了其他列

时间:2013-12-12 10:43:49

标签: session laravel laravel-4 logout

我想在user_id表格上添加额外的列session

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

有可能实现这个目标吗?

3 个答案:

答案 0 :(得分:2)

这是会话迁移架构:

Schema::create('sessions', function($table)
{
    $table->string('id')->unique();
    $table->text('payload');
    $table->integer('last_activity');
    $table->integer('user_id')->unsigned(); //// ADD IT!
});

你可以添加你喜欢的任何列,Laravel不介意。

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

$table->integer('user_id')->unsigned();

您可以为它创建一个模型:

class SessionModel extends Eloquent {

}

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

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

但是如果你正在考虑向有效载荷添加更多信息,这是Laravel保存会话数据的地方,尽管我认为这是可能的,你将不得不在Laravel的代码中挖掘更多信息来做到这一点。

答案 1 :(得分:0)

迁移的短版

if( Schema::hasTable('sessions') ) {
    $table->integer('user_id')->unsigned();
}

需要检查是否存在session表。或在之前创建它:

php artisan session:table
php artisan migrate

https://laravel.com/docs/5.7/session#introduction

答案 2 :(得分:0)

这是会话迁移模式:

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');
    });

要在将COLUMN添加到TABLE之后将信息添加到有效负载中,只需更改DatabaseSessionHandler类。

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

例如:

1º创建函数

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

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

    return $device;
}

2º在函数addRequestInformation中添加(&$有效负载)

protected function addRequestInformation(&$payload)
{
    if ($this->container->bound('request')) {
        $payload = array_merge($payload, [
            'ip_address' => $this->ipAddress(),
            'user_agent' => $this->userAgent(),
            'device' => $this->device(),
        ]);
    }

    return $this;
}

就绪,用户登录时设备已添加到表中。

相关问题