In my application, I am trying to save user information in a cache variable and access it in another method. I can access it inside the same method. But when I try to access out of that method, it returns null. I need to know how to achieve this, please someone help me out here with an example as I am new to the technology.

Inside a method:

Cache::set('user', $user);

Accessing it in another method:

$user = Cache::get('user');

And I want to clear it once accessing it.

1

There are 1 best solutions below

0
Harpal Singh On

Instead of using Cache for storing variable.

You can declare variable in class as following

class test{
    public $user = []; //declare your function here.

    function abc(){
        $this->user = $user; //Assign value to variable
    }

    function xyz(){
        $user = $this->user; 
        print_r($user);
    }

}