Laravel

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.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button