Getting and setting session in Laravel

Sessions in Laravel

Session:-As HTTP is state protocol.To maintain states on server and share data across multiple pages PHP session are used.PHP sessions are simple way to store data for individual users/client against a unique session ID.Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data,if session id is not present on server PHP creates a new session, and generate a new session ID.

In Laravel, the session configuration file is stored at config/ session.php.By default, Laravel is configured to use the file session driver, which will work well for many applications.

In Laravel you can use below function to play with session.


Setting session in Laravel

 public function showProfile(Request $request, $id)  {
    $request->session()->put('key', 'value');


Getting session in laravel

 public function showProfile(Request $request, $id)  {
//retrieve single key value from session   
 $value = $request->session()->get('key');

//retrieve all data from the session 
 $data = $request->session()->all();

//Checking if An Item Exists In The Session
 if ($request->session()->has('users')) {

Destroying session in laravel

 public function showProfile(Request $request, $id)  {
//Deleting Items From The Session




Leave a Reply

Your email address will not be published. Required fields are marked *