SoheilYou
SoheilYou

Reputation: 949

How to retrieve items from the cache in view in Laravel?

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?

Upvotes: 0

Views: 7134

Answers (3)

Purvesh
Purvesh

Reputation: 646

I would do it like this

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

 {{ $user[“name”]; }}

Upvotes: 0

Hasan Teymoori
Hasan Teymoori

Reputation: 192

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

Upvotes: 2

DevK
DevK

Reputation: 9952

Use the cache helper:

{{ cache('user')['name'] }}

Upvotes: 3

Related Questions