【问题标题】:How to store one variable in a cache inside one controller method and access it in another method in the same controller? - Laravel 6如何将一个变量存储在一个控制器方法内的缓存中,并在同一控制器的另一种方法中访问它? - Laravel 6
【发布时间】:2020-06-29 04:39:49
【问题描述】:

在我的应用程序中,我试图将用户信息保存在缓存变量中并以另一种方法访问它。我可以在相同的方法中访问它。但是当我尝试访问该方法时,它返回 null。我需要知道如何实现这一点,请有人帮我举个例子,因为我是这项技术的新手。

在方法内部:

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

用另一种方法访问它:

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

而且我想在访问它时清除它。

【问题讨论】:

  • 对此类问题使用类范围或单例模式。缓存与您尝试做的完全不同。

标签: laravel laravel-cache


【解决方案1】:

而不是使用缓存来存储变量。

你可以在类中声明变量如下

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);
    }

}

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 2015-08-02
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 2013-10-07
    相关资源
    最近更新 更多