0

I've stored some data in Laravel 5.5 cache in Service Provider as you can see in following:

class DataServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $user = Cache::rememberForever('user', function () {
            return array('name' => 'jack', 'age' => 25);
        });
    }

    public function register()
    {
        //
    }
}

I retrieve items from the cache in controller by this:

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

But I need to retrieve cache items within views (blade), How can I access them directly in views (blade) (without passing cache as variable)? I just want to store data in cache once, and access to it everywhere in my app with no more steps, is it possible?

4

3 回答 3

3

使用缓存助手

{{ cache('user')['name'] }}
于 2018-05-21T05:36:15.913 回答
1

缓存外观: {{ Cache::get('user')['name'] }}
缓存助手: {{ cache()->get('user')['name'] }}{{ cache('user')['name'] }}

于 2018-05-21T05:57:25.410 回答
0

我会这样做

@php 
    $user = Cache::get(“user”);
@endphp

 {{ $user[“name”]; }}
于 2018-05-21T06:04:29.073 回答