bukowski
bukowski

Reputation: 1943

How to create/update user_id in sessions table?

How to update/create user_id in sessions table in Laravel 5.2.

So I store sessions in a Database - here is the migration:

Schema::create('sessions', function ($table) {
    $table->string('id')->unique();
    $table->integer('user_id')->nullable();
    $table->string('ip_address', 45)->nullable();
    $table->text('user_agent')->nullable();
    $table->text('payload');
    $table->integer('last_activity');
});

Whenever user logs in with my own authentication I create a session like so:

$request->session()->put('user', ['name' =>' John', 'user_id' => 1 ]);

This doesn't set user_id in my sessions table, but I would like the user_id in my sessions table to correspond to my logged in user's id. So I can track in sessions table users by their user_id.

How can I set user_id in my sessions table?

Thanks!!

Upvotes: 2

Views: 1621

Answers (1)

Fadi Botros
Fadi Botros

Reputation: 111

To use database for session. First you must in the .env file set

SESSION_DRIVER=database

Then you should know that your saving is in the payload, not that it saves user_id that you tell. The user_id is the Auth::user()->id, not what you save

EDIT:

If you want to change the way laravel gets Auth::user(), you may decide to implement a UserProvider (very difficult)

Upvotes: 1

Related Questions