【问题标题】:Laravel session table add additional columnLaravel 会话表添加附加列
【发布时间】:2013-12-30 17:33:08
【问题描述】:

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

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

有可能实现吗?

【问题讨论】:

    标签: session laravel laravel-4 logout


    【解决方案1】:

    这是会话迁移架构:

    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 的代码中进行更多挖掘才能做到这一点。

    【讨论】:

    • @Antonio Carlos Ribeiro 谢谢。我处于同样的状态。我有疑问,你能告诉我吗,我计划一次从一个位置锁定登录,所以我正在继续使用数据库身份验证/会话驱动程序。因此,从另一个位置使用相同的凭据登录会显示一条消息“已经从另一个位置登录”。这种方法会好吗?谢谢
    • 当我尝试保存 sessionSQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' 时出现此错误
    • @EdmundSulzanok 如果为会话创建模型类,则需要确保包含public $timestamps = false;。否则,您的架构需要包含$table->timestamps();
    【解决方案2】:

    短版迁移

    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

    【讨论】:

      【解决方案3】:

      这是会话迁移架构:

      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(&$payload)

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

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

      【讨论】:

        猜你喜欢
        • 2015-02-23
        • 1970-01-01
        • 2021-07-14
        • 2016-07-22
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        相关资源
        最近更新 更多