Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rudolfbruder's avatar

Caching parts of view

Hi,

I am facing a challenge with caching parts of view. Our homepage consists of several @includes of other views in which some complex queries are happening. I am of course caching those queries but i would like to ask whether it is possible to somehow cache these 'static' parts of views - footer, header etc.

I cannot store view(...)->render() in cache because it would also cache the state of navbar which includes items in cart and profile icon.

Any ideas?

Thanks!

0 likes
14 replies
Sinnbeck's avatar

Can you show an example of something you wish to cache? And how do you want to cache

There is a command for caching views php artisan view:cache

martinbean's avatar

@rudolfbruder Why are queries happening inside view files in the first place? Controllers should be passing data to views.

rudolfbruder's avatar

@martinbean Of course i know..., Anyways we are using ViewComposers to store for example used categories of products on the homepage which is then shown a list. These are then used in one specific partial view which is included in the main 'view' by @include directive.

What i want to achieve is to have this part of the view saved in cache- pre rendered so that the not only the categories eloquent query is cached but also the html is cached.

@sinnbeck Imagine you have footer which has its links setup from backend cms part. It changes every month or so. I need to cache the whole footer blade file with the query result as well, not only the query result.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@rudolfbruder So save it to a html file and get that html file content and render it with

{!! $rawHtml !!}

You can just use file_put_contents and file_get_contents to save and load the html

1 like
rudolfbruder's avatar

@martinbean Indeed I am doing that as well. What i want to achieve is to cache the whole blade file in server ram so that it does not have to be rendered / processed every time for certain period of time.

rudolfbruder's avatar

@martinbean Well that command does not store the whole rendered view in server memory right? (redis / memcached) it just creates files under strorage/framework... right?

Sinnbeck's avatar

@rudolfbruder Sounds like a weird idea to store the html in redis, but should be easy enough using my suggestion. Sounds like a bad idea though.

$html = Cache::store('redis')->remember('awesomeHtml', $seconds, function () {
    return view('some-partial')->render();
});

MichalOravec's avatar

@rudolfbruder

What i want to achieve is to cache the whole blade file in server ram so that it does not have to be rendered / processed every time for certain period of time.

That's completely unnecessary, just cache your query and run php artisan view:cache on deployment, how Martin advice you.

1 like
rudolfbruder's avatar

@MichalOravec @martinbean @sinnbeck And to all others thank you for your time and answers, i know about the existence of php artisan view:cache. I wanted to ask you how to cache it directly into RAM as senior colleague of mine (OG php vanilla developer) suggested terms such as ESI include etc. We will probably end up with some sub-views being renders saved to cache and then passed to view as variables so that they do not have to be rendered on each request. I probably know it might be overkill in these ages of computers and 100gb of Ram on server but to be honest in was pretty interesting task and thing to think about.

Once again thanks. I will close this one soon, if you have any comments I would be more than glad to read them out :)

Please or to participate in this conversation.