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?
Deleting Session Data $request->session()->forget('key'); Use flush() method instead of forget() method to delete all session data.
Description ¶ session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
The session configuration is stored in config/session. php . Be sure to review the well documented options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for the majority of applications.
You can use the session helper without having to use a request object.
session()->forget('some_data');
session()->flush();
In Laravel 5.3 you still can use flush()
and forget()
methods:
session()->flush();
session()->forget('key');
https://laravel.com/docs/5.3/session#deleting-data
add session()->save();
after that.
To delete session variable in Laravel 5.6
session()->forget(['key1']);
to delete the session variables (More the one value delete from the session) use the arguments as arguments session()->forget([' ']);
session()->forget(['key1','key1','key3','...']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With