abu abu
abu abu

Reputation: 7028

Set & Get session variable value

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.

Upvotes: 0

Views: 2618

Answers (1)

Shekh Saifuddin
Shekh Saifuddin

Reputation: 508

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..

Upvotes: 2

Related Questions