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

uniqueginun's avatar

storing session data in database

I'm using file sessions driver in my app and I want to store data in my sessions table not for maintaining user session but just to track user login activity. where should I but my piece of code? I put it in listener for login event and the data inserted successfully but the session id stored in database table doesn't equal session id of the file.

Here's my handle function in my listener class

public function handle($event)
{

    try {
        $data = [
            'id' => session()->getId(),
            'user_id' => request()->user()->id,
            'ip_address' => request()->ip(),
            'user_agent' => request()->userAgent(),
            'last_activity' => Carbon::now()->getTimestamp()
        ];

        Session::updateOrCreate($data, $data);

    } catch (\Exception $exception) {

    }
}
0 likes
1 reply
uniqueginun's avatar
uniqueginun
OP
Best Answer
Level 9

solved. make middleware and store data from within it instead of event listener.

Please or to participate in this conversation.