Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Save extra field in Session db table on login

How can I save additional data when a user logs in to the session db table?

0 likes
16 replies
jlrdw's avatar

It will store in session like any other session data will.

eggplantSword's avatar

I have set up multiple login guards so I have also modified the session table by adding another field client_id so I want to make sure it saves in the session table but right now it's not being saved. How can I save the client_id into the session table?

jlrdw's avatar

You don't have to have an extra field for session to store something, that's part of the payload.

Are you referring to having extra field on the users table or another table, in which case you can just add a field.

1 like
eggplantSword's avatar

This is my sessions table

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

The user_id saves when logging in with a user, but when logging in with a client the client_id is null, I want to save it like the user_id gets saved.

This is what I have in config/auth.php

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'client' => [
            'driver' => 'session',
            'provider' => 'clients',
        ],

        'client-api' => [
            'driver' => 'token',
            'provider' => 'clients',
            'hash' => false,
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        'clients' => [
            'driver' => 'eloquent',
            'model' => App\Models\Client::class,
        ],
    ],

  'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'clients' => [
            'provider' => 'clients',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],
eggplantSword's avatar

That was the tutorial I followed, this is the login controller I made. Is this supposed to automatically save it into client_id on the sessions table?

public function login(Request $request)
    {
        $this->validator($request);

        if(Auth::guard('client')->attempt($request->only('email','password'),$request->filled('remember'))){
            //Authentication passed...
            return redirect()
                ->intended(route('client.home'))
                ->with('status','You are Logged in as Client!');
        }

        //Authentication failed...
        return $this->loginFailed();
    }
jlrdw's avatar

That doesn't need done on a sessions table, the client table is for the client guard if I am understanding you correct.

A session table is for using database session instead of file based session.

eggplantSword's avatar

My supervisor wants to save in the sessions table the logged in user or clients id as well as the regular sessions table information, that's what I'm trying to do. The user_id field works without any additional setup when a regular user logs in and I want to save the client_id when a client logs in.

jlrdw's avatar

In your other post you mentioned separate tables equating to another guard. When the guard is setup correctly, session data will be present.

The session table isn't permanent storage, it's for session data during a session on the site. The user data will be in users table, client data in clients table.

So I am not sure what you are wanting to do.

eggplantSword's avatar

I'm just trying to do what is being asked of me, I don't know the logic behind why doing it this way all I know is that I have to figure it out. From what I understand the users and clients come from different sources, the users are internal and the clients come from an erp online. I told my supervisor about the suggestions to use only one users table with user roles but he said that wouldn't work since the clients are coming from the erp, idk. My supervisor wants to store the session data along with who is logged in, be it a user or client by saving their id in the sessions table. That's what I'm trying to figure out, how to store a client_id when a client logs in.

bugsysha's avatar

@msslgomez just saying that is being asked of you is not the best approach to development. If you don't understand the problem and requirements there is almost a guarantee that you will miss to identify and implement the key features.

1 like
eggplantSword's avatar

What else am I supposed to do when asked to do something I don't know how to do? Not ask on here to find possible solutions? I can't just magically know how to do something new without asking or googling it. I'm trying to as least get something working so my supervisor can check my work, if I come back with "Everyone is telling me not to do this in this way" (like I already did, they told me we still have to do it this way) that's not really helpful either. If it's not possible than just say that.

eggplantSword's avatar

No, both fields user_id and client_id are null when logging in as a client. If I log in as a regular user then the field user_id does get the correct id.

Please or to participate in this conversation.