How to set a session variable In session.php file, I am using driver='database' and table='sessions' now I am struck with how to set a session for a variable.
Native sessions, ie. $_SESSION is supported by Laravel 5 ?
Settings sessions
session(['key' => 'value']);
// Or
session()->put('key', 'value')
Getting a value from a session
$value = session()->get('key');
// Alternatively you can pass in a default value
$value = session()->get('key', 'default);
Deleting a session
session()->forget('key');
Source: http://laravel.com/docs/5.1/session#basic-usage
For lazy man like me :
$value = session('key');
Showing a error - Undefined variable Session::put()
Import the session facade or use the syntax helmerdavila showed you
use Session;
class SomeClass {
Session::put('key', 'value', 'minutes');
}
There should be a way to access the native $_SESSION specially if you are sharing session with other applications that do not ship as Composer package or otherwise have no Facade support.
Anybody?
@bobbybouwmann What does the minutes do in Session::put('key', 'value', 'minutes')? I can't find anything about the third argument on Laravel documentation.
Can anyone say me where to write the session code?
session(['key' => 'value']);
please show me the full code.
In your controller or anywhere you want to store a session . You can also retrieve session's data in your blade by using session('key') .
Hope this helps !
Put a value in session
session()->put('key', 'value');
Example:
session()->put('email', '[email protected] ');
Get a value from session
$value = session()->get('key');
Example:
$email = session()->get('email');
Please sign in or create an account to participate in this conversation.