1

I am confused why it is prerequisite of creating a database table for session based on the documentation, when session can work without it (db table).

Someone could explain me,

  1. I can directly session(['key' => 'value']); without the table, why we need to create a database table for session (based on documentation)?
  2. There is a thing called Redis. what is the purpose? and how to I use (I already installed)?

  3. What are the purpose of the fields like user_id, ip_address, user_agent, payload, last_activity? and how can these be useful?

schutte
  • 1,949
  • 7
  • 25
  • 45
  • 1
    The file driver is handy for developing and testing but is not the best solution for production, can have issues even in a medium traffic website. – dparoli Aug 06 '19 at 07:51
  • @dparoli Now I know. the configuration of redis is quite difficult for me. I will be using database instead. – schutte Aug 06 '19 at 07:57
  • Database driver is good for production – dparoli Aug 06 '19 at 07:58

4 Answers4

2

The database and redis are just drivers to be used for storing session. By default laravel uses file driver which means you can still use session(['key' => 'value']); and that will be stored in file.

If you chose to use the database then you will need to create the tables which Laravel then uses to store those value in.

Redis is also a completely separate driver, and here is the wiki for it:

Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability.

So in conclusion you should chose which driver to use for your needs.

nakov
  • 13,938
  • 12
  • 60
  • 110
1

I have never used session in database, so I cannot answer all your questions as a fact. I think everyone will see/use this table with different approach or usage.

  1. The database table session is not a requirement. By default it uses the file storage. Using a database table allow you to manage the user sessions, (I suppose) sharing them between servers...
  2. Redis is like a database but with less complexity. It's only a stack of key => value where everything is stored in memory. It allows your application to store and retrieve data extremely fast. You also need to install redis-server and start a deamon on production or a worker when developing to make it work.
  3. They are probably nothing more than providing convenient information to work with (statistics, logs, and so on...)
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
1

All of the answers except the last one is in the documentation.

  1. The database table is only used when using the database driver for sessions. If you didn't change the driver then file storage is used and there is no need for the database table.

  2. Redis is an in memory data store that can be used to store session information and have very fast access to it. Predis is a php driver that can be used to connect to the Redis store, but you still need to install Redis on you server.

  3. These fields are used to identify the users and link session payloads to them.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
1

We need to change the session driver to the database or Redis.

it is based on our requirements Like I am using Heroku server for deployments and Free dyno of Heroku sleep after every 30 mins of inactivity. And also Heroku does not allow us to store the local files and it automatically refreshes the build once in a day. So, in that case, we cannot use the local file driver for the session and we have to change the session driver to the database or Redis.

I am using Redis as a session driver for my application.