Store, get and delete data in session laravel

In Web Development, storing data in session is a common practice. The session will store browser-specific data on server. It makes easy to handle data without storing it in a database.

Session is useful to protect the data that the user wouldn't be able to read or write. Sessions are one of the secure and easy ways to store data. Nowadays it's common to store login status and user data in session and retrieve them whenever required. Before starting let's understand what is session.

What is Session?

A session is the total time devoted to a particular task activity. In web development, use session begins when the user logs in and it will end after a per-defined time like 60 minutes or other if the user doesn't perform any activity. A session cookie is used in web pages for storing general information in case the user leaves the web page or browser. If user comes back at in specific time then it will use that session information to provide a better user experience.

Laravel has in-bulit session functions which will help you to develop grate functionality. Laravel provides various drivers like file, cookie, array, Memcached, Redis, and database to handle session data. Which means we can store session data into one of those types like if we have configured session storing as file then it will automatically stores data into files.

Below are some of Laravel's session drives with short description :

  • File : Session data are stored into files at storage/framework/sessions.
  • Database : It will store information into database table.
  • Cookie : Data is stored in secure, encrypted cookies.
  • Redis / Memcached : Session data is stored into fast cache based storage.
  • Dynamodb : Sessions information are stored in AWS DynamoDB.
  • array : sessions are stored in a PHP array.

You can use any above driver to store session data. But you need to configure it before using it. Default driver for application is either session or file.

All session related configuration data is stored into config/session.php file like driver, lifetime, encrepted or not, storage path and more. Let's have a look at configuration file.

For this example, we will use database as driver. So we need to create sessions table into our database. Let's assume your application is running with proper database configuration and continue. Here, we will use session:table command to generate migration file for session table. It's in-built command to create session migration file.

It will automatically create new file at migration directory. let's migrate it using below command :

Access Session Functionality In Laravel

There are two ways to access session functionality in laravel.

  1. Global Helper
  2. Request Instance

While using global helper we don't need to create any dependencies to use session functions. On other hand, we need to pass Request instance to our functions and access functions using object methods. In further, we will take both methods examples to store, retrieve or delete session data.

For this tutorial, we have create SessionTestController to perform operations. You can use any controller as per your requirement.

Retrieving Session Data In Laravel

Let's assume, we have stored user's name into session variable and we will retrieve it using both methods.

In above example, getSessionData() function will get user name from session using global and request method. Here we have also share example to get default value in case user name not found.

Here, we have just get specific value from session. But you can get all data from session using below example :

Both methods will returns all session data in laravel. you can use it according to you requirement.

Store Data In Session Laravel

To store data in session we can either use request instance's put method or global session helper. You can store any type of data into session like string, array, json data. Let's take an example for storing data into session.

Here while using global helper we pass data into key and value pair. While using request instance we will pass name and value as parameter.

You can also use push method to push a new value onto a session value which is an array.

Deleting Session Data In Laravel

The forget() or flush() method to delete session data. The forget method is used to delete specific values from session while other hand flush method will delete all session data.

With forget method, you can delete multiple values from session. Please See below example to delete session data.

Flash Session Data In Laravel

Sometimes we need to store some data and it's required just for single request then we can use flash() method. Flash method will store particular value into session for one time use and automatically delete it after first retrieve. Example for flash data is showing user message like login success.

Conclusion

Here, we have taken some examples to store, retrieve, delete and flash data into a session in Laravel. But there are plenty of other methods for a session like checking session has a particular value, or a particular key exists in session data, increment or decrement session value, and more. We will learn more about them in further tutorials


Share your thoughts

Ask anything about this examples