3

I'm trying to store details about each persons session in the database as shown here: http://laravel.com/docs/4.2/session#database-sessions, and now I have the table migrated and looking all good to go. However, I cannot actually find any documents that give help on how to use this table. I wish to track the last actions of each person and also count the people online.

Can anyone provide examples of usage of the session database, or links to documentation? I assumed that this table would be automatically managed by Laravel, but it appears I am incorrect.

1 Answer 1

5

Can anyone provide examples of usage of the session database, or links to documentation? I assumed that this table would be automatically managed by Laravel, but it appears I am incorrect.

The only thing incorrect is your assumption of being incorrect. The session database is a storage engine for Laravel's session system. Once you've setup the database and configured Laravel to use the session database, you can use the syntax in the session docs to save and get data that's associated with each individual user of your web based system.

So, you have step 1 -- your database table created.

Step 2 would be configuring the storage engine by editing

app/config/session.php

and changing this

'driver' => 'file',

into this

'driver' => 'database'

Once you've done that any call to the session's put method (or other "saving data" methods) will store data in this table.

Sign up to request clarification or add additional context in comments.

2 Comments

I have actually explored the files to figure this file already, however your answer still does help so will mark as helpful and correct answer. Now I'm looking to count how many sessions are in the session table, what could be the best method of this? I have tried count(Sessions:all()); but this returns 3, where only 1 entry is in the table.
@Darryl I believe the "all" method returns a list of all the values set for the current user, not all the sessions in the system. If you wanted to count all the sessions in the system I believe you'd need to resort to a RAW SQL query on the table. The session API is concerned with saving and getting information for a single user, not providing statistics about the sessions themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.