Laravel session table add additional column

北战南征 提交于 2019-12-07 17:31:26

问题


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?


回答1:


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.




回答2:


Short version of migration

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

Need to check if exist session table. Or create it before:

php artisan session:table
php artisan migrate

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



来源:https://stackoverflow.com/questions/20541251/laravel-session-table-add-additional-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!