Sabbir Ahmed

Sabbir Ahmed

1 month ago

Laravel Sessions 101: The Developer's Reference Guide

Learning session management is very essential knowledge for building web applications. Laravel as a framework of php, provides some very handy way to manage sessions. Knowing and understanding the uses session syntaxes will always help you to think and build functionalities faster. In this guide we will see some common uses of laravel session.

A short note


Session provides a key value storage to make data available across multiple requests. Laravel's session feature provides a convenient way to store information across multiple requests for a user. Alongside that, there are facade and helper function both available to access the built in methods to use session for managing data. 

Some common uses of laravel session 


Lets see some common uses of session in laravel for reference. I have shown both facade and helper functions for your convenience:

Storing Data in Session


To store new data in session:

// Using session() helper function
session(['key' => 'value']);

// Using Session facade
use Illuminate\Support\Facades\Session;

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

Retrieving Data from Session


To get the old data saved in session: 

// Using session() helper function
$value = session('key');

// Using Session facade
use Illuminate\Support\Facades\Session;

$value = Session::get('key');

Checking if a Key Exists in Session


you can use has() method: 


// Using session() helper function
if (session()->has('key')) {
    // Key exists in session
} else {
    // Key does not exist in session
}

// Using Session facade
use Illuminate\Support\Facades\Session;

if (Session::has('key')) {
    // Key exists in session
} else {
    // Key does not exist in session
}

Removing Data from Session


To remove data we can use forget(): 


// Using session() helper function
session()->forget('key');

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::forget('key');

Flashing Data to the Session


Flashing data will store temporarily in session for the next request only: 


// Using session() helper function
session()->flash('key', 'value');

// Using Session facade
use Illuminate\Support\Facades\Session;

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

Flashing Input to the Session


The flashInput() method in Laravel's session allows you to temporarily store input data in the session for the next request only. This is particularly useful when you need to redirect a user back to a form with their input pre-filled after a validation error or any other redirect scenario similar.


// Using session() helper function
session()->flashInput(['username' => $username, 'email' => $email]);

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::flashInput(['username' => $username, 'email' => $email]);

Reflashing All Session Data


The reflash() method in Laravel's session allows you to keep all flash data for an additional request. This means that flashed data will be available for two requests instead of just one.


// Using session() helper function
session()->reflash();

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::reflash();

Regenerating the Session ID



// Using session() helper function
session()->regenerate();

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::regenerate();

Storing an Array in Session


// Using session() helper function
session(['user' => ['name' => 'John Doe', 'email' => 'john@example.com']]);

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::put('user', ['name' => 'John Doe', 'email' => 'john@example.com']);

Retrieving an Array from Session


// Using session() helper function
$user = session('user');

// Using Session facade
use Illuminate\Support\Facades\Session;

$user = Session::get('user');

Storing an Object in Session


// Using session() helper function
$user = new stdClass();
$user->name = 'John Doe';
$user->email = 'john@example.com';
session(['user' => $user]);

// Using Session facade
use Illuminate\Support\Facades\Session;

$user = new stdClass();
$user->name = 'John Doe';
$user->email = 'john@example.com';
Session::put('user', $user);

Retrieving an Object from Session


// Using session() helper function
$user = session('user');

// Using Session facade
use Illuminate\Support\Facades\Session;

$user = Session::get('user');

Incrementing and Decrementing Data in Session


This  allows you to increment or decrement the value of a numeric session variable. 

// Using session() helper function
session()->increment('counter');
session()->decrement('counter');

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::increment('counter');
Session::decrement('counter');

Flush the Entire Session


With this, the whole session data will be flushed: 

// Using session() helper function
session()->flush();

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::flush();

Keeping Data in Session


// Using session() helper function
session()->keep(['key1', 'key2']);

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::keep(['key1', 'key2']);

Retrieving All Data from Session


// Using session() helper function
$data = session()->all();

// Using Session facade
use Illuminate\Support\Facades\Session;

$data = Session::all();

Manually Starting a Session


// Using session() helper function
session()->start();

// Using Session facade
use Illuminate\Support\Facades\Session;

Session::start();

You can save this article as a future reference to use it anytime. There are also plenty of other uses of session in php and laravel. Let me know if you have some other cases in your mind in the comment and please give a love to this article if you found it helpful. 

I wish you a happy day. 

Comments

Login As User
Word Count: 0