How to use session in laravel

Rakesh Sharma      23 Comments  

How to use session in laravel :-

Laravel provides more inbuilt method to get and set session data. it’s easy to working with session in laravel.A session variable is used to store some information or some data about user or anything you want to access on all pages of an application.In laravel session configuration is stored in “app/config/session.php”.
so let’s see how to use session in laravel.
Setting a single variable in session :-

syntax :- Session::put('key', 'value');
example :- Session::put('email', $data['email']); //array index
           Session::put('email', $email); // a single variable
           Session::put('email', ''); // a string

Retrieving value from session :-

syntax :- Session::get('key');
example :- Session::get('email');

Checking a variable exist in session :-

// Checking email key exist in session.
if (Session::has('email')) {
  echo Session::get('email');

Deleting a variable from session :-

syntax :- Session::forget('key');
example :- Session::forget('email');

Removing all variables from session :-


Working with array values :-

Setting an array in session :-

$data = Array ( [_token] => P1VsHQZQdpguhWN82n9znnfuvG2aAf7fCk8SJtUV [email] => [password] => 123456 ) 

Session::push('user', $data);  //$data is an array and user is a session key.

Getting an array item from session :-

//array in session looks like 
Array (
    [0] => Array
            [_token] => P1VsHQZQdpguhWN82n9znnfuvG2aAf7fCk8SJtUV
            [email] =>
            [password] => 123456

//check and getting above array

if (Session::has('user')) {
  $user = Session::get('user');
  echo $user[0]['email'];

pushing an index in existing array in session :-

Session::push('user.mytoken', 'rakesh');
//will be pushed in user array with new index "mytoken" like:-
    [0] => Array
            [_token] => P1VsHQZQdpguhWN82n9znnfuvG2aAf7fCk8SJtUV
            [email] =>
            [password] => 123456

    [mytoken] => Array
            [0] => rakesh

Now you have all set with basics of laravel session for more information about using session in laravel Follow laravel session.

  • anil kumar Pandey

    Great examples for using session in laravel, really its very helpful for me.

  • Raja Mishra

    I found another simple and easy tutorial of laravel session from

  • Nguyễn Quang Bình

    How to use ::flash() ??

    • rakesh sharma

      you can use like this Session::flash(‘key’, ‘value’);

  • hi how to use session?

    • rakesh sharma

      Are you facing any issue? or need any help?

  • meera

    how to get id using session in laravel 5?

    • rakesh sharma

      Try $session_id = Session::getId();

      • nimmy

        thank you

  • how to use search bar in laravel 5 ?

  • Gaurav Gawade

    hello, can any one help me out to use session please…

  • Tanvir142

    to use session like this, do i have to configure session in config/session.php?

    • Roni Estein

      no, that’s only required for persistent storage.

  • Jarvanux

    Thanks!! :D

  • Luciano Alexandre

    I am unable to access this session in another file

  • Davy

    Hello, sorry I’m new to Laravel. How do I load logged-in user data from a database into a session so as to use them in a view? :-(

  • Bi Híp


  • Alex

    How to get value from session without deleting? How to make forever session?

  • Hargovind Chaiwala

    How does this work in Laravel 5.2? Also, what namespace does Session actually belong to?

  • parvinder singh

    Thanks Nice tutorial

  • Uhelliton Andrade


  • Cristiano