Learn Laravel

Laravel Session

Like other frameworks Laravel provides inbuilt method to get and set session data. Laravel Session methods offering more flexibility for PHP developers.

Initializing a Session Variable

Session::put('key', 'value');

Retrieving value from Session Variable

Session::get('key');

Retrieving All Data From Session

Session::all();

Removing An Session Variable From The Session

Session::forget('key');

Destroying Session

Session::flush();

Regenerating Session ID

Session::regenerate();

Checking An Item Exists in Session.

<?php if (Session::has('key')) { } ?>

Example

In this example we will Push a value into session array.

<?php
$data =array('fname'=>'John','lname'=>'Mishra','email'=>'[email protected]')
$userdetail=Session::push('userdetail', $data);  //$data is an array and userdetail is a session key.
print_r(userdetail) ;//getting above array
?>