Laravel - How to use/access session variables in a view?

In case you have a session variable called $mySessionVar and inside you have key value pair information then you could use:

<p>
    @if(Session::has('$mySessionVar'))
        {{ Session::get('mySessionVar')['keyName'] }}
    @endif
</p>

And in case the value is an array (key/value pair) you could use:

@foreach(Session::get('mySessionVar')['keyName'] as $variableName)
    <span>{{ $variableName }} </span>
@endforeach

Just use session() helper to read the data:

{{ session('key') }}

Or:

{{ request()->session()->get('key') }}

If you want to store something, do this:

@php(session(['key' => 'value']))

Its quite simple as core php, you can use its facade in blade or a method like,

{{Session::get('key')}}

Or by method like,

{{ session()->get('key') }}

Tags:

Php

Laravel