Home >>Laravel Tutorial >Laravel Session

Laravel Session

Laravel Session

Way to store the information across the multiple user requests is by creating sessions to keep track of the users that visit the website. Below shown example of session in Laravel.

First we create a form naming form.blade.php for entering data.

<html>
<head> 
<title> Session <title>  
<head>  
<body>  
<form method="Post" action="session">
@csrf  
<div><label for="Name">Name</label>
<input type="text" name="username"></div><br>
<div><button type="submit">Submit </button></div>
</form> 
<body>  
Session In Laravel

Now make a form controller FormController.php and make store function inside the controller.

public function store(Request $request)  
{  
  print_r($request->input('username'));  
}   
at last define route for url
Route::get('/session',function()  
{  
  return view('form');  
});  
Route::post('/session','FormController@store');  
Session In Laravel

Store Data in Session

To store the session we use put() method as shown below.

$request->session()->put('user', $request->input('username'));

To retrieve the session get() session is used.

public function store(Request $request)  
{  
$request->session()->put('user', $request->input('username'));  
echo $request->session()->get('user');   
}  

Global Session

Global session function can also be used that stores and reads the value in a session. When the single parameter is passed to a session function, it returns the key value. And if array of key/value pair is passed, then the values are stored in the session.

Retrieve a data from session key

$data=session('key');

Provide a default value to the session key.

$data=session('key', 'default');

Store the value in the session key.

session(['key'=>'value']);

This can be done by adding following syntax to(FormController.php)

public function store(Request $request) 
{  
session(['user'=>$request->input('username')]);  
$data=session('user');  
echo $data;  
}  
Session In Laravel
Session In Laravel

Retrieve Session Data

To get all session data all() method is used.
$session_data = $request->session()->all();
Example is shown below(FormController.php)
public function store(Request $request) {  
session(['user1'=>'phptpoint']);  
session(['user2'=>'onetwothree']);  
return $request->session()->all();  
}  
web.php
Route::get('/show','FormController@store');
Session In Laravel

Delete Session Data

Session data can be deleted by the method forget() or delete()

Example is shown below(FormController.php)
{  
session(['user1'=>'phptpoint']);  
session(['user2'=>'onetwothree']);  
$request->session()->forget('user1');  
return $request->session()->all();  
}  
Session In Laravel

Above Screenshot shows only user2 session that means user1 session data is deleted.

Another method to delete all session is flush() method.

$request->session()->flush();
Example shown below of using flush() method(FormController.php)
public function store(Request $request)  
{  
session(['user1'=>'phptpoint']);  
session(['user2'=>'onetwothree']);  
$request->session()->flush();  
return $request->session()->all();  
}  
Session In Laravel

Above screenshot is showing empty array which means all sessions are deleted

Flash Data Using Session

Flash data is useful to store the data in the session for the current request as in the next request flashed data is removed.

Example shows below the flash data(FormController.php)
public function store(Request $request)  
{  
session()->flash('post', 'Name has been added');  
return $request->session()->get('post');  
}  
Session In Laravel
After removing flash() method, it will look something like this.
public function store(Request $request)  
{    
return $request->session()->get('post');  
} 
Session In Laravel

No Sidebar ads