How to use Session in Laravel
Hello artisans, today I’ll talk about how to use session in our Laravel Application. A session variable used to store some information which we can retrieve anywhere in our application. So, let’s see how we can use it in our application.
Note: Tested on Laravel 8.67.
Table of Contents
Store Data into session
Laravel provides a helper methods called session().We can easily store value easily by put() method
session()->put('key','value');
you can also push a value into an array session by push() method
session()->push('key','value');
Retrieve Data from session
Retrieving a value from the session is also easy like store like below you can retrieve the specific value a session
session()->get('key');
You can also retrieve all the session values by using the all() method.
session()->all();
You can also check if an item exists in the session and then you can fetch that, for that you have to use has() method.
if (session()->has('key')) {
// perform your logics here
}
Regenerating ID’s
If you want to regenerate all the IDs for the session, you can use the regenerate() method.
session()->regenerate();
Deleting Session Values
You can use forget() method forget a specific key from a session.
session()->forget('key');
You can also delete all the session items, for that you have to use flush() method.
session()->flush();
That’s all for today. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)