DEV Community

Cover image for Laravel Session Tutorial and Example
Code And Deploy
Code And Deploy

Posted on

Laravel Session Tutorial and Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-session-tutorial-and-example

In this post, I will show you about the Laravel 8 session with examples and the importance of sessions in programming. A session is the most useful when creating a web application, and it will handle storing data without saving it on the database.

The session is used to protect user data who currently view your web application, and it will be secured because it cannot be updatable or writable. The session is assigned with a unique id which is used to get the stored values. Even if the session is created, a cookie stored a unique session saved on the visitor's computer and will retrieve every request to the server.

Laravel framework provides easy ways to handle the session. Laravel sessions can be stored in databases, files, or encrypted cookies. Laravel session configuration can be found in config/session.php. By default, Laravel session data is stored in storage files. You can also use the database if you want to use it just change the driver in your .env file and run the following command below:


php artisan session:table

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Once you run the above command your Laravel session will store in the sessions table.

Storing Data in Laravel Session

Laravel provides a way of handling sessions. The first is to use the Request instance and the second uses the session() helper function.

// Request instance
$request->session()->put('key','value');

// global session helper
session(['key' => 'value']);
Enter fullscreen mode Exit fullscreen mode

Getting Data with Specified Key

If you want to get the session with your specific key use the following method below:

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

// global helper method
$value = session('key');

// return session with default value if the session key is not found
$value = session('key', 'default');
Enter fullscreen mode Exit fullscreen mode

If you need to display all data stored in the session, just run the following method.

$data = $request->session()->all();
Enter fullscreen mode Exit fullscreen mode

Checking session with key

To check if the specified key exists in the session, just run the following method.

if ($request->session()->has('users')) {
    $user = $request->session()->get('users');
}

Enter fullscreen mode Exit fullscreen mode

To check if the specified key session is null, run the following method.

if ($request->session()->exists('users')) {
    $user = $request->session()->get('users');
}
Enter fullscreen mode Exit fullscreen mode

Push Array Session Value

Laravel also provides a method to push value to the existing session array, This will set a new value for name key of a user array.

$request->session()->push('user.name', 'Taylor');

Enter fullscreen mode Exit fullscreen mode

Delete Laravel Session Data

To delete/remove a specific session key on Laravel just use the following method.

$request->session()->forget('key');
Enter fullscreen mode Exit fullscreen mode

Also if you think to retrieve and remove the session, then use the following method.

$value = $request->session()->pull('key', 'default');
Enter fullscreen mode Exit fullscreen mode

To delete multiple sessions. Just use the example below.

$request->session()->forget(['key1', 'key2']);
Enter fullscreen mode Exit fullscreen mode

To delete all session data. Just use the example below.

$request->session()->flush();
Enter fullscreen mode Exit fullscreen mode

We can manually regenerate session ID also just run the following method below. This method will prevent users to make malicious from exploiting a session fixation attack.

$request->session()->regenerate();
Enter fullscreen mode Exit fullscreen mode

To regenerate the session ID and remove all data from the session. Just use the example below.

$request->session()->invalidate();
Enter fullscreen mode Exit fullscreen mode

Now after reading this post you have a basic understanding of the *Laravel session *. I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-session-tutorial-and-example if you want to download this code.

Happy coding :)

Top comments (0)