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?

SoheilYou
  • 907
  • 5
  • 23
  • 43

3 Answers3

3

Use the cache helper:

{{ cache('user')['name'] }}
DevK
  • 9,597
  • 2
  • 26
  • 48
2

Cache Facade: {{ Cache::get('user')['name'] }}
cache helper: {{ cache()->get('user')['name'] }} or {{ cache('user')['name'] }}

Hasan Teymoori
  • 192
  • 1
  • 15
0

I would do it like this

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

 {{ $user[“name”]; }}
Purvesh
  • 628
  • 7
  • 12