13

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

Machavity
  • 30,841
  • 27
  • 92
  • 100

3 Answers3

14

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.

thchp
  • 2,013
  • 1
  • 18
  • 33
  • 1
    Thanks for your valuable answer @tete0148 indeed I realized these are helpers, and I agree. Even though I have not properly investigated why Laravel makes the session accessible separate from using the session itself. What brought about this observation is the text on Laravel docs itself about a **difference** they both have. So unfortunately, I would have to still check if I can find a difference then I can truthfully and sincerely say YES I agree. – Oluwatobi Samuel Omisakin Jul 19 '17 at 17:23
  • 1
    When I tried to check the core how the session is implemented with request base class, I got more convinced that the general session might be a bit different **in operation* as the http ones, i.e the one from the Request class. – Oluwatobi Samuel Omisakin Jul 19 '17 at 17:25
  • 1
    I clarified this in my response. It isn't just a short cut, it is rather advisable to stick to using either the global session() or the request()->session(). The document states it clearly that there's practical difference, indeed which is very noticeable while developing. So, your assertion is very wrong! – DAVID AJAYI Jan 29 '20 at 16:16
0

i think this would help you: $request->session() and session() both are same thing.

There are two primary ways of working with session data in Laravel: the global function in session() helper and via a $request instance.

you can use it like this

public function testMyMethod(Request $request){

    //$userExist = $request->session()->exists('user_id');
    $userExist = $request->session()->has('user_id');

}
Ahmad
  • 445
  • 5
  • 15
0

Unfortunately the best answer has already been given by the Laravel note; and I can only attest to that for now cos I have noticed such situation once.

I couldn't understand why global session('key') refuse to echo the value of $request->session()->put('key', 'value') within the same method. Hopefully I'd come across the situation again just for proof but the last response I'd want to give you is:

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

Cos just like the docs mentioned, there's difference notable in practice

DAVID AJAYI
  • 1,912
  • 20
  • 13
  • 1
    I think the answer to your question is because Laravel stores the session values just at the end of the request lifecycle. – T30 Jan 28 '20 at 15:14
  • Yeah that makes sense, maybe the reason stored values are not retrievable – DAVID AJAYI Feb 06 '20 at 19:25