How to get all session data in laravel

In this article, we will get to know, how to get session data in laravel, in a very easy way. I will show you step by step guide to getting session data in laravel.

There are many ways to get session data in laravel, I will show you to get the session data by the helper and by using the facade.

Example-1: By using the session helper

Now in the below code, we will get all the session() data by using the helper

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $allSessions = session()->all();
        dd($allSessions);
    }
}

Example:2 By using the session facade

Now in the below code, we will use the session facade, to get the data the session data.

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $allSessions = Session::all();
        dd($allSessions);
    }
}

How to get all session data in laravel blade

To get all the session data in your laravel blade file, use the below code in your blade file.

@dd(Session::all())

the above code will dd all the session value inside your blade file.

how to get particular session data in laravel blade

To get particular session data, we have to use get function with our session facade in our blade file.Use the below code to get the particular session data in laravel

{{ Session::get('your-key-name')}}

I hope now you understood the basic concept of using session in laravel, if you face any difficulties feel free to comment.

5 1 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Spread the love
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x