0

I am setting session variable in function of a controller like below.

use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    public function store(Request $request)
    {
        session(['user_name' => $user_name]);
    }
}

I am trying to access that session variable in another function of another controller.

use Illuminate\Support\Facades\Session;

class DashboardController extends Controller
{
    public function __construct()
    {
        dd(session('user_name'));   // I am not getting value here
    } 
}

I am not getting value from Session Variable.

1 Answer 1

2

You can do it like this

use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    public function store(Request $request)
    {
        session()->put('user_name', $user_name);
    }
}

And you can get it another controller or anywhere like this

session()->get('user_name');

Hope this will help you, thanks..

Sign up to request clarification or add additional context in comments.

1 Comment

@abu abu If this answer is helpful then please accept this answer by clicking the tick mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.