Search code examples
phplaravelsessionlaravel-5laravel-5.3

How to delete session in Laravel 5.3 without using Request?


I have a method in a controller that needs to handle the session. That method is called by a get method that doesn't require any user input, so I would like to do it without Request class.

Currently, I am able to set the session, but I cannot find a way to delete it. It looks something like this:

if ($boolean_storing_condition_value)
    session(['some_data'=>'Some Data']);
else
   /* What should be the unset function? */

In Laravel 4.2, it is done with Session::forget('some_data'); or Session::flush(). How should that be done in Laravel 5.3?


Solution

  • You can use the session helper without having to use a request object.

    session()->forget('some_data');
    session()->flush();