How To Handle Laravel Session Easily

How To Handle Laravel Session Easily

Laravel Session plays an important role in any web application, it helps you to save data in session which you can retrieve within your application.

In this post, we will look at how we can use the session helper methods in Laravel. Laravel Session provides many different format for saving session data which includes files, cookies, database, Redis and Memcached.

What is Session?

Sessions are a simple way to store data for each HTTP request against a unique session ID.

The session can provide the user activity data across your application. Session are normally sent to the browser via session cookies and the unique ID assigned against each session cookie can be used to retrieve session data.

Supported Session Drivers in Laravel

The default driver for the session are set to file when you create a new Laravel application. File drive is good for local development but for a production application, you should use the Redis or Memcached for the improved session performance.

You can change the session driver settings from the configuration file for the session located in config/session.php.

/*
    |--------------------------------------------------------------------------
    | Default Session Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the default session "driver" that will be used on
    | requests. By default, we will use the lightweight native driver but
    | you may specify any of the other wonderful drivers provided here.
    |
    | Supported: "file", "cookie", "database", "apc",
    |            "memcached", "redis", "dynamodb", "array"
    |
    */

    'driver' => env('SESSION_DRIVER', 'file'),

Using Laravel Session to Store Data

There are two different methods in Laravel Framework which you can use to set the value in the session. The first one is using the session helper function session().

// Global helper function

session(['key' => 'value']);

Secondly you can use the Request instance to set the value in session.

// Request class instance

$request->session()->put(['key' => 'value']);

Pusing Value to Session Array

The push() method of Request instance in used to push a fresh value to an array session.

// Request class instance

$request->session()->push(['key' => 'value']);

Retrieve Session Data

There are some chain methods available for the session().

Getting Session Value By Key

Similar to storing a session value, there are two different ways to retrieve a session value.

The first method is using the session() method. To retrieve a session value use the below method.

// Global helper function

$value = session('key');

Second method is to use the Request instance:

// Request class instance

$value = $request->session()->get('key');

Getting All Session Values

To get all session values, use the all() method on the Request instance like below.

// Get all values from session

$values = $request->session()->all();

Checking If a Session Value Exists

The has() method can be use to determine if a session value exists in the session. It will return true if a value exists and null if value doesn’t exists.

// Checking if a session value exist

if ($request->session()->has('key') {
//
}

Regenrating Session IDs

If you want to regenerate all IDs for the session, you can use the regenerate() method.

// Regenerate session IDs

$request->session()->regenerate();

Deleting Session Values

Use the forget() method to remove a specified item from the session, which takes a key argument.

// Remove an item from the session

$request->session()->forget('key');

To remove all items from the session, you can use the flush() method.

// Remove all values from session

$request->session()->flush();

Over To You

Sessions are very important for any web application, almost all web frameworks provide this functionality out of the box. In this post, we tried to cover all use cases for the Laravel Session.

Leave a Reply

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

*

When sharing a code snippet please wrap you code with pre tag and add a class code-block to it like below.
<pre class="code-block">you code here</pre>

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.