In this tutorial you will learn about the Laravel Session and its application with practical example.
What is Session?
In Laravel, session is a parameter passing mechanism which enable us to store data across multiple requests. Session allows us to keep track of visitor across application. Laravel uses a driver based system for session management, each of the driver is used to define where the session data will be stored. Laravel framework have following in-built session drivers –
file – Session data is stored in an encrypted file located at storage/framework/sessions.
cookie – Session data is stored in secure and encrypted user’s cookies.
database – Session data is stored in application database.
apc – Session data is stored in APC.
memcached – Session data is stored in Memcached.
redis – Session data is stored in Redis.
array – Session data is stored in a PHP array, but it is not persisted across the requests.
Laravel session configuration file is located at “app/config/session.php“. If you don’t specified your session driver, then by default file driver is used.
Storing Data In The Session
Using put method of Request instance we can store data in the session, it accepts two arguments the “key” and “value“.
Syntax:-
1 |
$request->session()->put('key','value'); |
Example:-
1 |
$request->session()->put('name','john'); |
Retrieving Value from Session
Using get method of Request instance we can retrieve a single value from the session, it accepts two arguments the “key_name” and “default_value“.
Syntax:-
1 |
$value = $request->session()->get('key_name'); |
OR
1 |
$value = $request->session()->get('key_name', 'default_value'); |
Second argument of the get method is the default value which will be returned if your specified key does not exist in the session.
Example:-
1 |
$name= $request->session()->get('name'); |
Retrieving All Session Data
Using all method of Request instance we can retrieve all the available data in the session.
Syntax:-
1 |
$all_data=$request->session()->all(); |
Checking If an Item Exists In Session
Using has method you can determine if a value is present in the session, it returns true if the value is present in the session and null if doesn’t present.
Syntax:-
1 2 3 |
if($request->session()->has('key_name')){ // } |
Example:-
1 2 3 |
if($request->session()->has('name')){ // statements to be executed if name exists } |
Deleting Data from session
Using forget method you can remove the specified item from the session, it accepts “key” as argument.
Syntax:-
1 |
$request->session()->forget('key'); |
If you want to retrieve an item value before deleting it from the session, you can use the pull method.
Syntax:-
1 |
$value = $request->session()->pull('key','default'); |
If you want to remove all items from the session, you can do it using flush method.
Syntax:-
1 |
$request->session()->flush(); |
Add Array In Session
If you want to add array into session, you can do it using push method as following –
Syntax:-
1 |
$request->session()->push('key', $arr_var); |
Example:-
1 2 |
$usr= array("name=> "Peter","email"=> "peter@w3adda.com", "ID"=> "135"); $request->session()->push('user_info', $usr); |
Regenerating Session ID
If you want to regenerate the session ID, you can do it using regenerate method.
1 |
$request->session()->regenerate(); |
Flash Data
If you want to store items in the session that will be available for only next request.You can do it using the flash method, flash data is available only for the subsequent HTTP request, and then will be deleted.
Syntax:-
1 |
$request->session()->flash('key', 'value'); |
Example:-
1 |
$request->session()->flash('status', 'Task was successful!'); |