Answer the question
In order to leave comments, you need to log in
What is the best way to cache views in Laravel 5.4?
There is a part of the page with large complex queries, I cache its query data in redis in this way:
$cache = cache("page-{$product->id}");
if (!$cache){
$view = view('product.index', ['product' => $product])->render();
cache(["page-{$product->id}" => $view], 180);
}else{
$view = $cache;
}
return $view;
Answer the question
In order to leave comments, you need to log in
It’s hard for you to write, it can be simpler:
$view = cache("page-{$product->id}", function () {
$view = view('product.index', ['product' => $product])->render();
cache(["page-{$product->id}" => $view], 180);
return $view;
});
return $view;
I support SvizzZzy and I can't find the right answer anywhere.
If I don’t want to cache the entire page, but I want to cache only a separate html piece, then how to organize it, because the view creates the entire page, and if you cache separate collections of models, then the cache is actually clogged with unnecessary information.
How to display a page consisting of separate cached blocks?
A good example is the site menu, which is the same for all pages, rarely changes, but is compiled by large queries in the database and links of different nesting levels of pages and categories. If I need to cache the top menu, content and footer of the site separately, how can I do this and then display it in the view?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question