Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the real difference between Laravel's session() and request()->session()?

I am working on a Laravel(5.2) project that heavily relies on session, quite new though but I was just curious what difference global session() and Http request()->session() has apart from the fact that they have different means of accessing and writing into session?

Here are the few information I have about this from laravel 5.4 doc,

enter image description here

Unfortunately, this does not really make me understand the difference. I have as well googled and stackoverflowed perhaps I could find an answer to no avail. Example is laravel difference of session::flash and request->session->flash but I am not so comfortable with the answer

What is the real difference they have in managing session data? I wouldn't mind a reference to a documentation where this is or even if I have to dig into laravel core.

Thanks

like image 986
Oluwatobi Samuel Omisakin Avatar asked Jul 12 '17 13:07

Oluwatobi Samuel Omisakin


People also ask

What is session in laravel8?

Sessions are used to store information about the user across the requests. Laravel provides various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used because it is lightweight. Session can be configured in the file stored at config/session.

How do I increase my session lifetime in laravel?

If you want to increase your session life time then we need to change in . env file and it is very easy to change it from configuration file in laravel. laravel provides you session. php file there is we can see 'lifetime' key option for setting time in minutes.

What is session PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.


1 Answers

session() is a helper that gives you a faster access to request()->session()

Note that request() is also a helper that gives you a faster access to the request object.

There are no differences, it's just a shortcut.

like image 60
thchp Avatar answered Oct 07 '22 01:10

thchp