phplaravelsessionlaravel-4logout

Laravel session table add additional column


I want to add an extra column user_id on session table.

The reason is, some time there are spammer to sign up fake account, once I know that the user is spammer, I want to log the user out by deleting the session record.

Is it possible to achieve this?


Solution

  • This is the session migration schema:

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

    You can add whatever column you like on it, Laravel won't mind.

    Or you can create a new migration and make it add a column on that table.

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

    You can create a Model for it:

    class SessionModel extends Eloquent {
    
    }
    

    And do whatever you need with it:

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

    But if you're thinking about adding more info to the payload, which is where Laravel keeps the session data, although I think it's possible, you'll have to dig a bit more in Laravel's code to do that.