is it possible to write output of view function to another blade file?
As the title says, i would like to render a page from view() function to save in another blade file. Then serve that blade file from a route. So render a blade file with variable for page1.blade.php then put the output in page2.blade.php
Reason behind: My page1 gets data from an external api, which stays same throughout the day. So i dont want to hit the endpoint every time someone loads the page. So i will generate the page once in the morning, then serve it all day. Is there an easier way to do this?
You're thinking about the problem from the wrong angle - the data is consistent, not the rendered view. Fetch the data from the external API and cache it. Then render the view using the cached data.
$data = cache()->remember('data_from_external_api', today()->addDay(), function () {
return Http::get(/* third party url */)
// etc
});
return view('name.of.view', compact('data'));
My page1 gets data from an external api, which stays same throughout the day. So i dont want to hit the endpoint every time someone loads the page. So i will generate the page once in the morning, then serve it all day. Is there an easier way to do this?
@reaz Yes, there’s an easier way. Just use caching instead of this convoluted workaround.
Cache the results the first time you make a request to the API:
$results = Cache::remember('api_results', Carbon::today()->endOfDay(), function () {
// Do API call here...
});
return view('some.view', [
'results' => $results,
]);
The API will only be hit once and its results cached until the end of the current day.