Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 745
cytsunny Avatar asked Jan 26 '17 07:01

cytsunny


People also ask

How do I delete a specific session in Laravel?

Deleting Session Data $request->session()->forget('key'); Use flush() method instead of forget() method to delete all session data.

How do I delete 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.

How we can delete session in PHP?

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.

Where is Laravel session stored?

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.


4 Answers

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

session()->forget('some_data');
session()->flush();
like image 176
Jerodev Avatar answered Oct 24 '22 07:10

Jerodev


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

like image 45
Alexey Mezenin Avatar answered Oct 24 '22 07:10

Alexey Mezenin


add session()->save(); after that.

like image 20
Jishnu RS Avatar answered Oct 24 '22 07:10

Jishnu RS


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','...']);
like image 39
Nadeem Qasmi Avatar answered Oct 24 '22 07:10

Nadeem Qasmi