Laravel Session

The session is used to store small information for a limited time. You can pass session value across the different pages within same domain or website.

Laravel Session Example


Laravel provides different drivers or types to handle the session. You can configure session using different drivers. 

A driver can be file session, cookie session, array session, Memcached, Redis, APC or database driver.

You do not need to handle $_COOKIE variable manually. Laravel do it by the smart way. Laravel provides some default method to handle the session. One more thing if you want to change session driver you have to configure session.php inside config folder.

Default file driver Laravel use. Let's take an example how you can create the session. 

// OR
// OR

Laravel store your session into key and value pair. You can use "Session" class or session() method both will work. Additionally, also you can use Request class object to handle the session. Once you store session you can get it using get() method.

// OR
// OR

You can remove session by using forget method.

// OR

To remove all session you can use flush() method

// OR

To get all value of session you can use all() method.

// OR

You can store anything in a session that can be a string, number of the even array also. Here take an example:

class HomeController extends Controller
    public function index(Request $request)
        Session::put('framework','Laravel'); // To set Session Using Session class
        Session::get('framework'); // To get Session Using Session class
        session()->put('version',5.4); // To set Session Using session() method
        session()->get('version'); // To get Session Using session() method
        $request->session()->put('features', array('Method Injection','Authentication Scaffolding','Routing Requesut')); // To set Session Using session() method
        $request->session()->get('features'); // To get Session Using session() method

        // To remove session you can use forget() method

        // To remove all session you can use all() method


If you are using file session You can take note that Laravel store unique file into storage/framework/sessions directory.

A directory contains the number of file with a unique name. Your information stored into serialize format into a file.

You can check that session is exists or not by using has() method. You can also get a session on blade file.

// welcome.blade.php
{!! (session()->has('version') ? session()->get('version') : '') !!}
// output 

Here I checked if a session exists then a print value of it.

Laravel also provide session flash. Flash session is auto expire once page refresh. Flash means single time use of session.

You can set session flash by using flash() method in Laravel 5.4.

class HomeController extends Controller
    public function index(Request $request)
        // session flash example

To access it on view page same you can use session get() method.

Tutorial By Gautam Patadiya